using timeit for a function in a class

F

flupke

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
 
F

Fredrik Lundh

flupke said:
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>
 
K

Kent Johnson

flupke said:
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
 
F

flupke

Fredrik said:
:




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




</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
 
F

flupke

Kent said:
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
 
F

Fredrik Lundh

flupke said:
? 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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top