Google News
logo
Python Program to Measure the Elapsed Time in Python
You can use the `time` module in Python to measure the elapsed time between two events. Here's an example code that demonstrates how to use the `time` module to measure elapsed time :
Program :
import time

start_time = time.time()

# Your code here

end_time = time.time()

elapsed_time = end_time - start_time
print("Elapsed time: ", elapsed_time, "seconds")
Output :
Elapsed time:  1.1920928955078125e-06 seconds
In this code, we first import the `time` module. Then, we use the `time.time()` function to get the current time and store it in the `start_time` variable.

After that, we write our code that we want to measure the elapsed time for. This could be any code that you want to measure the time taken for.

Once our code is done, we get the current time again using `time.time()` and store it in the `end_time` variable.

Finally, we calculate the elapsed time by subtracting `start_time` from `end_time`, and store the result in the `elapsed_time` variable. We then print out the elapsed time using `print()` function.

This code will output the elapsed time in seconds.