timeit.repeat() inaccuracy on W2K?

Raymond Hettinger vze4rx4y at verizon.net
Wed Nov 26 01:13:16 EST 2003


"Dennis Benzinger" <Dennis.Benzinger at gmx.net> wrote in message
news:3fc3da6d$1 at news.uni-ulm.de...
> I just played around with the new timeit module.
> Using the following code I get some strange results:
>
> import timeit
>
> def test(s):
>      result = 0
>      for c in s:
>          result += ord(c)
>      return result
>
> t = timeit.Timer("test('dennisbenzinger')", "from __main__ import test")

Perhaps there is something awry with the default windows timer, so try this
instead:

 t = timeit.Timer("test('dennisbenzinger')", timer=time.time, "from __main__
import test")



> But sometimes I get something like
>  >> [3.3410785448988629, 4802.7882074397721, 1203.1983464378854]
> where one or two execution times are much higher than the other
> ones, although the program took about the same time to execute
> and certainly not some thousand times as long...

I once saw an issue like this and the underlying cause turned out to be having
two tick counters on a multi-processor machine.


Raymond Hettinger






More information about the Python-list mailing list