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("t est('dennisbenz inger')", "from __main__ import test")
print t.repeat(number =50000)
Most of the time I get a result like [3.3263199652469 799, 3.3338471789012 294, 3.3557058229467 716]
with all execution times are about 3.
But sometimes I get something like [3.3410785448988 629, 4802.7882074397 721, 1203.1983464378 854]
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 1 1659
"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("t est('dennisbenz inger')", "from __main__ import test")
Perhaps there is something awry with the default windows timer, so try this
instead:
t = timeit.Timer("t est('dennisbenz inger')", timer=time.time , "from __main__
import test")
But sometimes I get something like >> [3.3410785448988 629, 4802.7882074397 721, 1203.1983464378 854]
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> import time
>>> t=timeit.Timer("pass","pass",time.time)
>>> print t.timeit()
|
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)
|
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"
|
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()
|
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 ?...
| |
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)
|
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__
|
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):
|
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
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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,...
|
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...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |