0

I have the following code snippet below. I have print statements so it'll print what part of the code it's currently working through in the console window. I was wondering if there was a way to add the time it took to calculate each block of code in the print statements as well?

print('Starting Backtest Instantiation')
backtester_example = Backtest(new_data_sample, position_logic_example)

print('Running Output DF')
aggstats = backtester_example.agg_stats()
stats_df = backtester_example.output_all_metrics_as_df()

print('Running Tabular Trades')

compact_data = backtester_example.tabular_trades()
print('FinishedTabularTrades')
novawaly
  • 1,051
  • 3
  • 12
  • 27
  • look into `timeit` module – r.ook Oct 24 '18 at 17:08
  • Possible duplicate of [How do I get time of a Python program's execution?](https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution) – r.ook Oct 24 '18 at 17:09
  • @Idlehands what is the difference betwen using timeit and doing a start=time.time() end = time.time() and subtracting them? – novawaly Oct 24 '18 at 17:11
  • Depends on your use case. `timeit` allows you to run *n* tests with a simple [`timeit.timeit(... number=n)` function](https://docs.python.org/3/library/timeit.html). I *think* `timeit` is more specific for unit tests, but if you're only concerned about real time performance it shouldn't make a difference AFAIK. – r.ook Oct 24 '18 at 17:16

0 Answers0