This uses the function sage_timeit().
Time execution of a command or block of commands.
Displays the best WALL TIME for execution of the given code. This is based on the Python timeit module, which avoids a number of common traps for measuring execution times. It is also based on IPython’s %timeit command.
TYPICAL INPUT FORMAT:
timeit(statement, preparse=None, number=0, repeat=3, precision=3)
sage: timeit('2^10000') 625 loops, best of 3: ... per loop
We illustrate some options:
sage: timeit('2+2',precision=2,number=20,repeat=5) 20 loops, best of 5: ... per loop
The preparser is on by default (if it is on), but the preparse option allows us to override it:
sage: timeit('2^10000', preparse=False, number=50) 50 loops, best of 3: ... per loop
The input can contain newlines:
sage: timeit("a = 2\nb=131\nfactor(a^b-1)", number=25) 25 loops, best of 3: ... per loop
This eval function is called when doing %timit in the notebook.
OUTPUT: string – timing information as a string
sage: timeit.eval("2+2") # random output '625 loops, best of 3: 1.47 us per loop'
We emphasize that timeit times WALL TIME. This is good in the context of Sage where commands often call out to other subprocesses that don’t appear in CPU time.
sage: timeit('sleep(0.5)', number=3) # long time (5s on sage.math, 2012) 3 loops, best of 3: ... ms per loop