timeit.timeit and timeit.repeat give different answers

Dan Christensen jdc at uwo.ca
Sun Jul 11 14:30:29 EDT 2004


The test below is done with Python 2.4a1 compiled from source, but
the same thing happens with Debian's python2.3_2.3.4-2.

Python 2.4a1 (#1, Jul 11 2004, 12:20:32) 
[GCC 3.3.4 (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> import time  
>>> t=timeit.Timer("pass","pass",time.time)
>>> print t.timeit()
0.0560228824615
>>> print t.repeat(1)      
[0.042751073837280273]

The results are consistent if repeated, i.e. timeit always produces
about 0.056 and repeat always produces about 0.043.

It doesn't help to do:

  for i in range(10):
    print t.timeit()

vs.

  print t.repeat(10)

The problem happens on both machines I've tested it on.  One is a
desktop.  No other jobs running.

With Python 2.3, the results are affected by the setting of the
PYTHONPATH environment variable.  With it unset or set to an empty
directory /tmp/foo I get results like above.  With it set to an empty
directory /tmp/python, I get results that agree with each other.

But with Python 2.4, all three settings give results that disagree.

If I use the ipython shell, things are even worse:  the results
disagree by a factor of almost 2!

I've read timeit.py and can't see how this might happen.

Any thoughts?

Dan




More information about the Python-list mailing list