function_to_test(test, mylist): #test as an int, mylist as a list of int
do_something
return something
function_generates_a_list(min, max): #min, max are int
do_something
return a_List
I need to timeit the function_to_test and only this one. The purpose is to test code in order to determine which one is the best to test if a int is in a list of int. (test is a googolplex and mylist is a huge list starting from min to max). Yes, I already know the result of the function, I need to know how many times it will cost.
My problem is: which syntax have I to use with timeit if I want to pass a global list as a local parameter of a function I test?
The easiest way to do this is to create a function that takes no arguments, and calls your function with whatever arguments it needs:
You can then call
timeit(newfunc). You could also do this with a lambda expression –timeit(lambda: function_to_test(mylist))is exactly equivalent to the above. If you’re usingtimeitmodule-wise (ie, doingpython -m timeit ...), you could also do this:You could avoid having
newfuncaltogether by having a sufficiently convolutedsetupstring (the call tofunction_generates_a_listhas to be in setup, possibly (as here) but not necessarily by happening when you import the module, if you don’t want it to be part of your timings), but having the function makes things more flexible and less annoying.