This is the outline of a simple program
# some pre-defined constants
A = 1
B = 2
# function that does something critical
def foo(num1, num2):
# do something
# main program.... do something to A and B
for i in range(20):
# do something to A and B
# and update A and B during each iteration
import timeit
t = timeit.Timer(stmt="foo(num1,num2)")
print t.timeit(5)
I just keep getting "global name foo is not defined"..... Can anyone help me on this? Thanks!
The functions can use arguments in
timeit
if these are created using closures, we can add this behaviours by wrapping them in another function.or shorter, we can use functools.partial instead of explicit closures declaration
I usually create an extra function:
I prefer creating a
static
class with all the Data ready to be picked up prior of running the timer.Another option is to bind the function to its arguments via functools (similar to std::bind). Then you don't need to pass arguments to timeit, the callable returned by
functool.partial
takes care of that:Supposing that your module filename is test.py