timeit.repeat() inaccuracy on W2K?

D

Dennis Benzinger

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")
print t.repeat(number=50000)

Most of the time I get a result like
>> [3.3263199652469799, 3.3338471789012294, 3.3557058229467716]
with all execution times are about 3.

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'm running Python 2.3.2 on Windows 2000 with ServicePack 4
 
R

Raymond Hettinger

Dennis Benzinger said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top