27: Measure Elapsed Time for a Python Program Execution

You can make use of the time module in order to measure how much execution time your Python program took.

Let's take a look at an example.



Program: Find Program Execution Time 
in seconds sing Python

Author  : Code2care.org
Version : v 1.0
Date    : 3 July 2023 


import time

program_start_time = time.time()

#Program logic
sum = 0
sum = (333*244*245*134*34523525)/((28323424*23883)*39482983)

program_end_time = time.time()

# Calculate the elapsed time
elapsed_time = program_end_time - program_start_time

# Print the elapsed time
print("Elapsed Time:", elapsed_time, "seconds")

When I do multiple runs, I get different outputs.

Elapsed Time: 0.00014829635620117188 seconds
Elapsed Time: 0.00012087821960449219 seconds
Elapsed Time: 0.00014448165893554688 seconds
Elapsed Time: 0.0001227855682373047 seconds
Elapsed Time: 0.00014138221740722656 seconds

Must Read:

1. time module (Time access and conversions) : https://docs.python.org/3/library/time.html#

2. time.time(): https://docs.python.org/3/library/time.html#time.time

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap