I need to time the execution of a function across variable amounts of data.
def foo(raw_data):
preprocessed_data = preprocess_data(raw_data)
time = timeit.Timer('module.expensive_func(preprocessed_data)', 'import module').timeit()
However, preprocessed_data
is not a global variable. It cannot be imported with from __main__
. It is local to this subroutine.
How can i import data
into the timeit.Timer
environment?
The module function timeit.timeit(stmt, setup, timer, number) accepts four arguments: stmt which is the statement you want to measure; it defaults to ‘pass’. setup which is the code that you run before running the stmt; it defaults to ‘pass’.
The following article discusses the way to return a local variable created inside a function, which can be done by returning a pointer to the variable from the called function to the caller function. What happens when you try to return a local variable as usual?
timeit runs your snippet of code millions of times (default value is 1000000) so that you get the statistically most relevant measurement of code execution time! timeit is pretty simple to use and has a command-line interface as well as a callable one.
For a single iteration exec. time, divide the output time by number. The program is pretty straight-forward. All we need to do is to pass the code as a string to the timeit.timeit () function. It is advisable to keep the import statements and other static pieces of code in setup argument.
Pass it a callable to time, rather than a string. (Unfortunately, this introduces some extra function call overhead, so it's only viable when the thing to time swamps that overhead.)
time = timeit.timeit(lambda: module.expensive_func(data))
In Python 3.5 and up, you can also specify an explicit globals
dictionary with a string statement to time:
time = timeit.timeit('module.expensive_func(data)',
globals={'module': module, 'data': data})
The accepted answer didn't work for me inside pdb
debugger and a class method. The solution that worked is to add the variables to globals()
:
globals()['data'] = data
globals()['self'] = self
timeit.timeit(lambda: self.function(data))
Note that the timing overhead is a little larger in this case because of the extra function calls. [source]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With