measure time taken for python program

How to Measure Time Taken By Python Program to Execute

Python developers often need to measure the amount of time taken by python function, script or module to execute. This is an important step to identify performance bottlenecks and improve performance. In this article, we will learn a couple of ways to measure time taken by python program to execute.


How to Measure Time Taken By Python Program to Execute

We will use two different python libraries time and timeit to measure execution time for python code.


1. Using time

In this approach, we simply use python’s time library to get current time at the start and end of our python code and print their difference. This way we will get the amount of time passed during python code execution.

Here is the basic code format to measure time taken.

# Code to Measure time taken by program to execute.
import time
  
# store starting time
begin = time.time()
  
# program body starts

# enter your code here
# program body ends
  
# store end time
end = time.time()
  
# total time taken
print(f"Total runtime of the program is {end - begin}")

Here is an example of the above code.

# Code to Measure time taken by program to execute.
import time
  
# store starting time
begin = time.time()
  
# program body starts
  
for i in range(5):
    print("Hello")
# program body ends
  
# store end time
end = time.time()
  
# total time taken
print(f"Total runtime of the program is {end - begin}")

When you run the above code, you will see an output of format.

Hello
Hello
Hello
Hello
Hello
Total runtime of the program is 0.0010437965393066

This is a pretty easy to use approach and flexible enough to be used inside a function, module or full-fledged python script.


2. Using timeit

timeit module is used to specifically measure execution time. In this case, we first import the module into our code.

# importing the required module 
import timeit 

Next, we add code which we need to execute only once within our code to be executed.

# code snippet to be executed only once 
mysetup = "from math import sqrt"

Please note, we save these codes as strings, within quotes. timeit module will execute it later. Next we add the code to be executed, again as string.

# code snippet whose execution time is to be measured 
mycode = ''' 
def example(): 
    mylist = [] 
    for x in range(100): 
        mylist.append(sqrt(x)) 
'''

Finally, we call the timeit function to measure execution time.

# timeit statement 
print timeit.timeit(setup = mysetup, 
                    stmt = mycode, 
                    number = 10) 

In the above code, we pass mysetup string as setup argument, to be executed once. We pass mycode string as stmt argument to be executed after setup. We also pass number=10 to execute stmt 10 times. timeit will run mysetup once, mycode 10 times and return the execution time, which we print using print statement.

Here is the complete code for your reference.

# importing the required module 
import timeit 

# code snippet to be executed only once 
mysetup = "from math import sqrt"

# code snippet whose execution time is to be measured 
mycode = ''' 
def example(): 
    mylist = [] 
    for x in range(100): 
        mylist.append(sqrt(x)) 
'''

# timeit statement 
print timeit.timeit(setup = mysetup, 
                    stmt = mycode, 
                    number = 10) 

In this article, we have looked at two different ways to measure time taken to run function. It is useful to measure time elapsed, for your python code. It is important to remember that the execution time can vary depending on system/server load.

Calculating elapsed time for code is useful for identifying performance issues in your code. If you want to simply calculate execution time for a code block function or module, you ca use the first method mentioned above. However, if you want to perform detailed testing and need to run a piece of code multiple times to understand its average run time, use timeit.

Also read:

How to Encrypt & Decrypt Files in Python
How to Recursively Download Files & Folders in Wget
Bash Script to Run Commands on Remote Server
How to Add Text to Image in Python
How to Find & Copy Files in Linux

Leave a Reply

Your email address will not be published.