I’m trying to use the timeit module but I don’t know how.
I have a main:
from Foo import Foo
if __name__ == '__main__':
...
foo = Foo(arg1, arg2)
t = Timer("foo.runAlgorithm()")
print t.timeit(2)
and my Class Foo has a method named as runAlgorithm()
the error is this:
NameError: global name ‘foo’ is not defined
What am I doing wrong?
Can I take the time from a class method?
Instead of using the necessary
setupparameter for setting up the timeit environment, you can simply pass the method (or anything that is callable):From the documentation:
If you need to pass some arguments, you can use function currying with
functools.partial, for example: