python: slow timeit() function
Asked Answered
I

4

19

When I run the code below outside of timeit(), it appears to complete instantaneously. However when I run it within the timeit() function, it takes much longer. Why?

>>> import timeit
>>> t = timeit.Timer("3**4**5")
>>> t.timeit()
16.55522028637718

Using: Python 3.1 (x86) - AMD Athlon 64 X2 - WinXP (32 bit)

Irredentist answered 10/8, 2009 at 23:9 Comment(2)
Paging Alex Martelli, Alex Martelli, please comment on python timeit module...Alannaalano
RichieHindle already got it right while I was offline. And, I always use "python -mtimeit" anyway, never timeit within the interactive interpreter or a program;-)Bedad
P
33

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 = 1000
print t.timeit(count) / count
Pleasure answered 10/8, 2009 at 23:12 Comment(6)
Yep! I also recommend using python -mtimeit from a command line instead of timeit as a module -- the command line version has just too many handy little things you don't get from the module (repeating a varying number of times depending on how slow/fast is what you're measuring, for example: a great microbenchmarking technique, pity the module doesn't offer it -- etc, etc;-).Bedad
@Alex Your comment here isn't clear to me. Can you clarify what extra functionality is available on the CLI.Emphasize
@Tshepang: CLI use of timeit picks the number of repetitions "magically" -- few for slow code being measured, more for faster code, a lot for very fast code -- nicely balancing accuracy and time it takes to get results.Bedad
@Alex: now that's totally kool. Thanks for sharing. This piece of info should be more explicitly stated a bit better in the lib reference. Anyways, you also made it sound like there's other nifty features; what are those?Emphasize
@Alex: "the command line version has just too many handy little things you don't get from the module" was as if there was more; maybe it's nitpicking, but can you clarify that part (or just edit it out).Emphasize
@Tshepang: Run python -mtimeit -h to get full documentation. (And Alex can't edit his comment - comments are only editable for a short time after they are first created.)Pleasure
E
6

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.

Eddo answered 10/8, 2009 at 23:13 Comment(0)
E
4

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.

Emulsifier answered 10/8, 2009 at 23:18 Comment(0)
B
2

Timeit runs for one million loops by default.

You also may have order of operations issues: (3**4)**5 != 3**4**5.

Babin answered 10/8, 2009 at 23:18 Comment(1)
Thanks everyone. Very helpful. And yes, that was the intended order of operations.Irredentist

© 2022 - 2024 — McMap. All rights reserved.