3

In iPython, the %timeit magic function does a great job with its heuristics to determine a reasonable number of iterations to perform when timing a statement. The timeit docs suggest that this automatic determination of the number of loops is only possible from the command line.

Is that really the case? Or is there a workaround (or a generally accepted/common hack) that accomplishes the "magic" behavior while in code?

What I'd like to achieve is something like:

timer_results = [cool_timeit_function(stm) for stm in statement_list]

where cool_timeit_function would automatically decide whether to run 1 loop, or 10, or 100, etc.

I suppose it wouldn't be too hard to write something where you execute a given statement once, then if that happens very fast, you do it 10x, and if that happens very fast you do it 100x, etc...up until the execution takes, say, > 0.1 seconds. Then you could do a proper repeated timing of 10x that number of repetitions (so that the final timing run takes between 1s and 10s) or something like that. You could even specify how long you would want the final timing run to take and how many suites of that multiplicity you should do to take a "beset of" result.

But I can't be the first person to want this. Can anyone point to an existing solution?

8one6
  • 13,078
  • 12
  • 62
  • 84
  • err... the ipython source is open. maybe you could just look up the %timeit magic? try this, line 876... https://github.com/ipython/ipython/blob/master/IPython/core/magics/execution.py – Corley Brigman Jan 22 '14 at 19:29

0 Answers0