Suppose I have some function that takes an array and changes every element to be 0.
def function(array):
for i in range(0,len(array)):
array[i] = 0
return array
I want to test how long this function takes to run on a random array, which I wish to generate OUTSIDE of the timeit test. In other words, I don't want to include the time it takes to generate the array into the time.
I first store a random array in a variable x and do:
timeit.timeit("function(x)",setup="from __main__ import function")
But this gives me an error: NameError: global name 'x' is not defined
How can I do this?
Import x
from __main__
as well:
timeit.timeit("function(x)", setup="from __main__ import function, x")
Just like function
, x
is a name in the __main__
module, and can be imported into the timeit
setup.