473,799 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

timeit.timeit and timeit.repeat give different answers

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",t ime.time)
print t.timeit() 0.0560228824615 print t.repeat(1)

[0.0427510738372 80273]

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

Jul 18 '05 #1
3 2084
Can anyone confirm whether this discrepancy happens with other
installations of python on other hardware/OS's? It's a bit
disconcerting.

Thanks,

Dan

Dan Christensen <jd*@uwo.ca> writes:
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",t ime.time)
print t.timeit() 0.0560228824615 print t.repeat(1)

[0.0427510738372 80273]

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

Jul 18 '05 #2
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.0541989803314 20898 t.repeat(1) [0.0538489818572 99805] t.timeit() 0.0617580413818 35938 t.repeat(1) [0.0531320571899 41406] t.timeit() 0.0553798675537 10938 t.repeat(1) [0.0747120380401 61133] tt = t.repeat(1000)
tt.sort()
tt[0] 0.0530700683593 75 tt[-1] 0.0565237998962 40234


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
Jul 18 '05 #3
Dan Christensen wrote:
Hmm, if I type this at the python prompt:

r = []
for i in range(1): # Note: only one loop!
ti = t.timeit()
r.append(ti)
print r

I get data that agrees with t.repeat but disagrees with:
....
t.timeit()


You could try "intermedia te simplifications " to spot the culprit:

r = [None]
for i in range(1):
r[i] = t.timeit()

and

for i in range(1):
ti = t.timeit()

I've no idea what's happening.

Peter

Jul 18 '05 #4

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

Similar topics

1
1659
by: Dennis Benzinger | last post by:
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
0
423
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 timeit(stmt, setup='from __main__ import *', n=1000): t = timeit.Timer(stmt,setup)
5
2116
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
2703
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 = t.timeit()
2
1772
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 pasted) with the same results (Win2k, Python 2.4). Any help is appreciated. The output follows: #1 C:\Python24\Lib>timeit.py 'if hasattr(int, "__nonzero__"): pass' Traceback (most recent call last): File "C:\Python24\Lib\timeit.py", line 285, in ?...
2
3309
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 to use from the command line, but it is less convenient for timing large pieces of code or when working in the interactive interpreter. E.g. variations on this *don't* work: $ python Python 2.4.3 (#1, Jun 13 2006, 11:46:08)
3
5530
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 = timeit.Timer("callFunc(line, i)","from __main__
7
6426
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 in a class? class FetchUrlThread(threading.Thread):
7
1999
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
10484
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9072
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.