473,404 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

timeit.repeat() inaccuracy on W2K?

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

Jul 18 '05 #1
1 1643
"Dennis Benzinger" <De**************@gmx.net> wrote in message
news:3f********@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
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Christensen | last post by:
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) on linux2 Type "help",...
0
by: Michele Simionato | last post by:
I use a little wrapper to timeit: $ cat timeit_.py import timeit, __main__, warnings warnings.filterwarnings('ignore', 'import \* only allowed at module level',SyntaxWarning) def...
5
by: rurpy | last post by:
Why doesn't the following work? It generates a "NameError: global name 'data' is not defined" error. import timeit global data data = env = "global data; x = data"
2
by: Steven D'Aprano | last post by:
When using the timeit module, you pass the code you want to time as strings: import timeit t = timeit.Timer("foo(x, y)", \ """from module import foo x = 27 y = 45 """) elapsed_time =...
2
by: 3c273 | last post by:
Hello, I was reading the thread on try/except overhead and decided to try Alex's examples but they kept generating exceptions. So I went to the docs and tried the examples there (copied and...
2
by: Steven D'Aprano | last post by:
The timeit module is ideal for measuring small code snippets; I want to measure large function objects. Because the timeit module takes the code snippet argument as a string, it is quite handy...
3
by: silverburgh.meryl | last post by:
Hi, I have a function in my python like this: def callFunc(line, no): # some code And I want to do a performance test like this: for line in f: for i in range(int(count)): t1 =...
7
by: silverburgh.meryl | last post by:
Hi, I am using timeit to time a global function like this t = timeit.Timer("timeTest()","from __main__ import timeTest") result = t.timeit(); But how can i use timeit to time a function...
7
by: ssecorp | last post by:
I am not clear about the results here. from timeit import Timer import Decorators def fib(n): a, b = 1, 0 while n: a, b, n = b, a+b, n-1
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.