In this tutorial, we will learn about Python elapsed time measurement using step by step guide. In Python, to optimize any code, we must know the total execution time taken by the code. Based on execution time and other related factor, we optimize the code. Hence, Python provides several built-in modules to calculate the execution time of any Python code. This is refer to as elapsed time. We will learn more about elapsed time and techniques to calculate it in the upcoming sections.
What is Elapsed Time in Python?
In Python, elapsed time refers to the duration that has passed between two specific points in time. It is often used to measure the execution time of a piece of code, the time taken by a function to run or any other time-related calculations within a Python program.
Python Elapsed Time [Step by Step Guide To Measure Elapsed Time]
There are several modules available in Python that can be used to calculate elapsed time. These are time, timeit and datetime modules. Each module provides a different class with different set of features to calculate the elapsed time. Let us see these modules one by one to calculate elapsed time using example programs.
Prerequisites
- Basic understanding of Python programming
- Any operating System installed
- Python3 installed
Using time Module
In Python, time module provides a time() function that returns the current time in seconds since the epoch. By taking timestamps before and after a specific operation, you can calculate the elapsed time. In the below example, we will find the prime numbers between the range(2, 100). We have set the timer and stored in variable ‘start_timer’ before starting the code to find the prime number. Once done, we again have set the timer and stored in variable ‘end_timer’. At the end elapsed_timer variable will store the elapsed time by finding the difference between end_timer and start_timer.
import time start_timer = time.time() prime_numbers = [] for i in range(2, 100): for j in range(2, i): if i % j == 0: break else: prime_numbers.append(i) print("Prime numbers between 1-100 are: ", prime_numbers) end_timer = time.time() elapsed_time = end_timer - start_timer print(f"Total execution time taken: {elapsed_time:.7f}")
> python3 .\prime-number.py Prime numbers between 1-100 are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] Total execution time taken: 0.0002427
Using timeit Module
In Python, timeit module provides a convenient way to measure the execution time of small bits of Python code. It has a default_timer class that alllows you to measure the time taken by a specific statement or function. In the below example, we will find the fibonacci series below 100. Similar to previous example, we have set the time before starting the code to find the fibonacci series and at the end of code. Finally, the difference between end_timer and start_timer will return the elapsed time.
from timeit import default_timer as time start_timer = time() fib_series = [0, 1] while True: val = fib_series[-1] + fib_series[-2] if val < 100: fib_series.append(val) else: break print(f"Fibonacci series below 100 are: {fib_series}") end_timer = time() elapsed_time = end_timer - start_timer print(f"Time taken to find Fibonacci series below 100 is: {elapsed_time:.7f}")
> python3 .\fib-series.py Fibonacci series below 100 are: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] Time taken to find Fibonacci series below 100 is: 0.0003567
Using datetime Module
In Python, the datetime module provides a datetime class that can be used to represent points in time. By subtracting two datetime objects, you can calculate the time difference between them. In the below example, we will find the duplicate elements in a given list. Set the timer same way as done in previous two examples to find the elapsed time.
from datetime import datetime start_timer = datetime.now() list_elements = [2,5,6,1,2,4,6,8,10,1] duplicate_elements = [x for x in list_elements if list_elements.count(x) > 1] duplicate_elements = list(set(duplicate_elements)) print(f"Duplicate elements are: ", duplicate_elements) end_timer = datetime.now() elapsed_time = (end_timer - start_timer).total_seconds() print(f"Total time taken to find the duplicate elements in the list are: {elapsed_time:.7f}")
OUTPUT
> python3 .\list-duplicate.py Duplicate elements are: [1, 2, 6] Total time taken to find the duplicate elements in the list are: 0.0000190
Summary
Read more about Python’s different time modules from python.org official documentation.