timeit.timeit and timeit.repeat give different answers

Peter Otten __peter__ at web.de
Tue Jul 13 13:56:30 EDT 2004


Dan Christensen wrote:

> Can anyone confirm whether this discrepancy happens with other
> installations of python on other hardware/OS's?  It's a bit
> disconcerting.

Measured on an aging Suse 8.1:

Python 2.3.3 (#1, Jan  3 2004, 13:57:08)
[GCC 3.2] on linux2
Type "help", "copyright", "credits" or "license" for more informa                                 
tion.
>>> import timeit, time
>>> t = timeit.Timer()
>>> t.timeit()
0.054198980331420898
>>> t.repeat(1)
[0.053848981857299805]
>>> t.timeit()
0.061758041381835938
>>> t.repeat(1)
[0.053132057189941406]
>>> t.timeit()
0.055379867553710938
>>> t.repeat(1)
[0.074712038040161133]
>>> tt = t.repeat(1000)
>>> tt.sort()
>>> tt[0]
0.053070068359375
>>> tt[-1]
0.056523799896240234
>>>

And now what? 

I'd just stick with the commandline's "best of N" strategy. Also, I would
expect the "pass" statement to be the fastest to execute and therefore the
least accurate to measure.

Peter



More information about the Python-list mailing list