By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,597 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

using timeit for a function in a class

P: n/a

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
Share this Question
Share on Google+
5 Replies


P: n/a
"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.