python: slow timeit() function python: slow timeit() function python python

python: slow timeit() function


The timeit() function runs the code many times (default one million) and takes an average of the timings.

To run the code only once, do this:

t.timeit(1)

but that will give you skewed results - it repeats for good reason.

To get the per-loop time having let it repeat, divide the result by the number of loops. Use a smaller value for the number of repeats if one million is too many:

count = 1000print t.timeit(count) / count


Because timeit defaults to running it one million times. The point is to do micro-benchmarks, and the only way to get accurate timings of short events is to repeat them many times.


According to the docs, Timer.timeit() runs your code one million times by default. Use the "number" parameter to change this default:

t.timeit(number=100)

for example.