Algorithm
- Import the
time
module. - Record the start time using
time.time()
and store it in a variable (e.g.,start_time
). - Perform the operations for which you want to measure the elapsed time.
- Record the end time using
time.time()
and store it in a variable (e.g.,end_time
). - Calculate the elapsed time by subtracting the start time from the end time (e.g.,
elapsed_time = end_time - start_time
). - Print or use the elapsed time as needed (e.g.,
print(f"Elapsed Time: {elapsed_time} seconds")
).
Code Examples
#1 Code Example- Python Programing Using time module
Code -
Python Programming
import time
start = time.time()
print(23*2.3)
end = time.time()
print(end - start)
Copy The Code &
Try With Live Editor
Output
52.9
3.600120544433594e-05
3.600120544433594e-05
#2 Code Example-Python Programing Using timeit module
Code -
Python Programming
from timeit import default_timer as timer
start = timer()
print(23*2.3)
end = timer()
print(end - start)
Copy The Code &
Try With Live Editor
Output
52.9
6.355400000000039e-05
6.355400000000039e-05
Demonstration
Python Programing Example to Measure the Elapsed Time in Python-DevsEnv