How to Measure the Execution Time of a Python Program?

In programming languages, for measuring the performance of code or script the execution time of the program is calculated. The execution time is the time taken by the program for executing the instructions written in the code. There are various ways to compute the execution time of the Python program. Some of them are discussed in this tutorial.

In this blog post, various methods are utilized to measure the execution time of the Python program. 

Method 1: Using time.time() Function of time Module

In the below code, the “time” module’s function named “time.time()” is used to calculate the execution time of a program:

Code:

import time
start_time = time.time()
x = 0
for value in range(9999999):
    x += value
end_time = time.time()
print(end_time - start_time)

In the above code:

  • The “time” module is imported into the program.
  • A floating-point number is returned by the “time.time()” function at the beginning of the code as the starting timestamp.
  • Similarly, the “time.time()” function calculates the timestamp at the end of the code.
  • To get the execution time of the program the difference between the “final timestamp” and “starting timestamp” is calculated.

Output:

The above snippet shows the execution time of the given program.

Method 2: Using timer() Function of timeit Module

In the bellow code the “timer()” function of the “timeit” module is used to calculate the execution time of the program:

Code:

from timeit import default_timer as timer
start_time = timer()
num = 32
num1 = 32
print(num*num1)
end_time = timer()
print(end_time - start_time)

In the above code:

  • The “default timer” function is imported from the “timeit” module at the beginning.
  • The “timer()” function calculates the timestamp at the beginning of the code before executing the expression and the end of the code after executing the expression.
  • The difference between the ending and starting is calculated to find the execution time of the program.

Output:

The above snippet shows the calculated execution time of the program.

Method 3: Using datetime Module

In the below code the “time.monotonic()” function is used to calculate the execution time of the program and the “timedelta()” function represents the difference between starting and ending time:

Code:

import time
import datetime
start_time = time.monotonic()
x = 0
for value in range(9999999):
    x += value
end_time = time.monotonic()
output = datetime.timedelta(seconds=end_time - start_time)
print(output)

In the above code:

  • The “time” module along with the datetime module is imported at the beginning.
  • The “time.monotonic()” function is utilized to find the value of a monotonic clock before the execution of statements and after the execution of statements.
  • The “timedelta()” function takes the seconds argument and calculates the difference between the ending and starting time of a program. The function will return the program execution time.

Output:

The snippet shows the execution time in seconds.

Method 4: Using time.process_time_ns()

In the below code the “time.process_time_ns()” function is utilized to find the CPU execution time of the given program:

Code:

import time
start = time.process_time_ns()
x = 0
for value in range(9999999):
    x += value
end = time.process_time_ns()
print (end - start)

In the above code:

  • A function within the “time” module called “time.process_time_ns()” calculates the sum of the user’s CPU time of the current process and the system time.
  • The “time.process_time_ns()” function is used to calculate the execution time at the start and end of the program.
  • Lastly, the final execution time is calculated by subtracting the start time from the end time.

Output:

The above output shows the “CPU” execution time of a program.

Conclusion

To compute the execution time of the program, the “time.process_time_ns()” function, “time” module, “timeit” module, and “datetime” module are used in Python. The “time” module provides a function “time.time()” that is utilized to measure the program execution time. Alternatively, other approaches such as the “datetime” module, the “time.process_time_ns()” function, etc., are used to calculate the execution time of a program very efficiently and smoothly. This blog presented a detailed guide on how to measure the execution time of a program.