473,403 Members | 2,293 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,403 software developers and data experts.

using timeit for a function in a class


Hi,

i tried to use timeit on a function in a class but it doesn't do what i
think it should do ie. time :)
In stead it starts printing line after line of hello time test!
What am i doing wrong in order to time the f function?

class TimeTest(object):
def f(self):
print "hello time test!"

if __name__ == '__main__':
from timeit import Timer
s = """
test = TimeTest()
test.f()
"""
t = Timer(s,"from __main__ import TimeTest")
print t.timeit()

Regards,
Benedict Verheyen
Jul 19 '05 #1
5 1656
"flupke" wrote:
i tried to use timeit on a function in a class but it doesn't do what i
think it should do ie. time :)
In stead it starts printing line after line of hello time test!
What am i doing wrong in order to time the f function?
how do you expect timeit to figure out how long it takes to run your
function without calling the function?
class TimeTest(object):
def f(self):
print "hello time test!"


</F>

Jul 19 '05 #2
flupke wrote:

Hi,

i tried to use timeit on a function in a class but it doesn't do what i
think it should do ie. time :)
In stead it starts printing line after line of hello time test!
What am i doing wrong in order to time the f function?


Hmm, by default Timer.timeit() calls the function being timed 1000000 times. That's a lot of "hello
time test" =:-)

Kent
Jul 19 '05 #3
Fredrik Lundh wrote:
"flupke" wrote:

i tried to use timeit on a function in a class but it doesn't do what i
think it should do ie. time :)
In stead it starts printing line after line of hello time test!
What am i doing wrong in order to time the f function?

how do you expect timeit to figure out how long it takes to run your
function without calling the function?

class TimeTest(object):
def f(self):
print "hello time test!"

</F>


? i think you missed some of the code
....
s = """
test = TimeTest()
test.f()
"""
....

So the function is being called (as i said, it prints the hello message).

Regards,
Benedict
Jul 19 '05 #4
Kent Johnson wrote:
flupke wrote:

Hi,

i tried to use timeit on a function in a class but it doesn't do what
i think it should do ie. time :)
In stead it starts printing line after line of hello time test!
What am i doing wrong in order to time the f function?

Hmm, by default Timer.timeit() calls the function being timed 1000000
times. That's a lot of "hello time test" =:-)

Kent


Hehe, thanks Kent.
Need to drink more coffee and train the eyes more, a new goal in life :)

print t.timeit(1) did the trick.

Regards,
Benedict
Jul 19 '05 #5
"flupke" wrote:
? i think you missed some of the code
...
s = """
test = TimeTest()
test.f()
"""
...

So the function is being called (as i said, it prints the hello message).


timeit is a benchmark utility. it's supposed to call your function
enough times to get an accurate result.

if you want to time a single call, using timeit instead of time is
just plain silly.

</F>

Jul 19 '05 #6

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

Similar topics

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()
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...
2
by: kevin | last post by:
Hi... I'm having a few problems here... I want to input in my function different values and then time each one to see how long it took to run the function... so right now it doesn't like the "i"...
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
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
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.