Python Program - Measure the Elapsed Time in Python

here's a Python program that measures the elapsed time between two points in a program:

import time

# get the current time
start_time = time.time()

# do some task
for i in range(1000000):
    pass

# get the current time again
end_time = time.time()

# calculate the elapsed time
elapsed_time = end_time - start_time

# print the elapsed time
print("Elapsed time:", elapsed_time, "seconds.")
Sour‮‬ce:www.theitroad.com

In this program, we first import the time module using the import keyword.

We then use the time.time() function to get the current time and store it in the variable start_time.

We then perform some task - in this case, we just run a loop for a million iterations. This is just to simulate some time-consuming task.

After the task is complete, we get the current time again using the time.time() function and store it in the variable end_time.

We then calculate the elapsed time by subtracting the start time from the end time, and store it in the variable elapsed_time.

Finally, we use the print() function to print out the elapsed time in seconds. Note that the elapsed time will depend on the speed of your computer and the nature of the task being performed.