Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing a local variable in a function into timeit

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?

like image 438
EMiller Avatar asked Sep 10 '14 15:09

EMiller


People also ask

How do you use timeit in Python?

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’.

How to return a local variable created inside a function?

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?

How many times does timeit run my code?

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.

How to calculate time in Python for a single iteration?

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.


2 Answers

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})
like image 200
user2357112 supports Monica Avatar answered Nov 14 '22 22:11

user2357112 supports Monica


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]

like image 38
Dennis Golomazov Avatar answered Nov 14 '22 23:11

Dennis Golomazov