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!
foois being defined in a different scope thant...globals=globals()parameter to the timeit call: stackoverflow.com/a/51913199/2925963duration,return_value= timeit.Timer(functools.partial(your_function_name, argument_1, argument_2,argument_3)).timeit(1)where your_function name isfooif you write a function like:def foo(argument_1, argument_2, argument_3):. Also include the:timeit.template = """..block from @Hugh Bothwell. And thereturn_valueis what the function returns, for exampleargument_1 + argument_2. And use:import timeit, functools. This solution is given by @Jose Ricardo Bustos M.