Algorithm


  1. Import the time module.
  2. Record the start time using time.time() and store it in a variable (e.g., start_time).
  3. Perform the operations for which you want to measure the elapsed time.
  4. Record the end time using time.time() and store it in a variable (e.g., end_time).
  5. Calculate the elapsed time by subtracting the start time from the end time (e.g., elapsed_time = end_time - start_time).
  6. 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

x
+
cmd
52.9
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

x
+
cmd
52.9
6.355400000000039e-05
Advertisements

Demonstration


Python Programing Example to Measure the Elapsed Time in Python-DevsEnv