469,949 Members | 2,165 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,949 developers. It's quick & easy.

module timeit and variable scope

A couple questions about using timeit to record the CPU time consumed
by an instance method:

1. What are the advantages of using timeit compared to using
time.time() or time.clock()? (on Mac OS X)

2. How do I introduce a variable into the timeit.Timer? For example I
have a class Foo:

class Foo:
def __init__ (self):
# the following line didn't work because the scope is not
# the calling function
# self._timer = timeit.Timer(stmt='self.run(bar)')
pass

def someMethod (self, bar):
print bar

def run (self, bar):
# call self.someMethod(bar) and record time using
# self._timer
pass
and I want to record the CPU time consumed by someMethod in method
run(), using the timer initialized in __init__(). The reason to set up
the timer in __init__ is to avoid overhead of setting up the same
timer time and time again in run().

Thank you.
Jul 18 '05 #1
0 921

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Roy Smith | last post: by
5 posts views Thread by ChaosKCW | last post: by
2 posts views Thread by Steven D'Aprano | last post: by
7 posts views Thread by silverburgh.meryl | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.