473,383 Members | 1,737 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,383 software developers and data experts.

Simplified timeit


I've been avoiding the timeit module because it seems
overly complicated. I finally coded my own function
(named timeit :), which has the following benefits:

1. Simple
2. Figures out how many times to evaluate the
callable passed to it.

Here's the code:

from time import clock

def timeit(f, time=0.5):
"""Calls f many times to determine the average time
to execute f.
Usually accurate to within 3%."""
count = 1
while True:
start = clock()
for i in range(count): f()
end = clock()
T = end - start
if T >= time: break
count *= 2
return T / count

Example:

from math import *

def f(x): return x * x - cos(x)
def g(): return f(1.0)

t = timeit(g)
print 'Time to execute f: ', t
print 'Calls to f per second: ', 1.0/t

PS: Try repeating this benchmark with psyco. I get a
70x increase in speed.

Connelly Barnes

__________________________________
Do you Yahoo!?
Friends. Fun. Try the all-new Yahoo! Messenger.
http://messenger.yahoo.com/

Jul 18 '05 #1
0 1225

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

Similar topics

0
by: Connelly Barnes | last post by:
I've been avoiding the timeit module because it seems overly complicated. I finally coded my own function, which has the following benefits: 1. Simple 2. Figures out how many times to evaluate...
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",...
2
by: Roy Smith | last post by:
I'm playing with the timeit module, and can't figure out how to time a function call. I tried: def foo (): x = 4 return x t = timeit.Timer ("foo()") print t.timeit()
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.