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 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>
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
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
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
"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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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()
|
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...
|
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"...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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...
| |