Measure performance of Python Code - Profiling code
Are you in a situation where you want to measure the performance of your code block? Whether its a function call or for loop or just a few lines of code?
It's fairly simple to do this in python. Thanks to python for providing inbuilt methods which can be used to measure the performance or elapsed time for a code block.
NOTE: There is an older way to use time.clock()
function in python that works <
python 3.3. It's deprecated since python 3.3 version.
So, we use the approach of timeit()
to measure the time of a code block
Measure the time to execute a function using timeit()
Python's timeit() is a method in Python library to measure the execution time of a code. This python lib runs the code snippet 1 million times(1,000,000 times!) and gives the minimum execution time of the code snippet. This method helps to measure the execution time of a code.
Syntax of timeit method
timeit.timeit(statement, setup, timer, number)
Parameters explanation
- statement: code snippet that you are going to measure the performance. The default value is "pass".
- setup: setup details in case there is any, before the code snippet or statement is executed. The default value is "pass".
- timer: this will have a timer value, in case you want to pass something custom.
timeit
already has a builtin timer, so, we can skip this parameter. - number: the code snippet will execute this
number
of times. The default value is 1000000.
Working with timeit()
is fairly simple, you just have to import
timeit and use it with a statement.
Example of timeit on a simple python statement
Here is a simple example-
import timeit
print(timeit.timeit('a = 5'))
Output of the above code is as shown below, it is the total time it takes to run the testcode
0.0134670734406
Here we have a simple code statement a = 5
and we see that the time taken to execute this piece of code is 0.0134670734406
.
Example of profiling code on multiple-lines or a block of code
You can time multiple lines of code, by separating them using a semicolon. Here is an example to demonstrate the same-
import timeit
print(timeit.timeit('a = 5; b=3; sum=a+b'))
Output of the above code is as shown below, it is the total time it takes to run the testcode
0.0287630558014
Example of profiling function for measuring performance of a function or method
You can time multiple lines of code, by having them inside a triple quote block. Here is an example to demonstrate the same-
import timeit
import_module = "import random"
testcode = '''
def test():
return random.randint(0, 25)
'''
print(timeit.repeat(stmt=testcode, setup=import_module))
Output of the above code is as shown below, it is the total time it takes to run the testcode
0.0555720329284668
Why is timeit() the best approach to profile code and measure execution time of a python code?
There are several reasons, but, here are a few why timeit() is the best approach to measure execution time of a python code-
- It executes the code snippet 1 million time (it's the deafult value). So, there are high chances you won't see an anamoly or random value.
- Garbage collection is disabled every time this is executed. So, thereby giving you apt result.
timeit()
internally uses the right methods based on your operating system i.e.,time.clock()
for Windows Operating System andtime.time()
for Linux and MAC.
Profiling code and measing the time of a block of code is as easy as this! Happy Coding!