import time
# Start timer
start_time = time.time()
# Code to be timed
# ...
# End timer
end_time = time.time()
# Calculate elapsed time
elapsed_time = end_time - start_time
print("Elapsed time: ", elapsed_time)
In Python, you can use the time module to measure elapsed time. You can use the time() function to get the current time in seconds since the epoch (January 1, 1970, at 00:00:00 UTC). Then, you can subtract the start time from the end time to get the elapsed time.
Alternatively, you can use the perf_counter() or monotonic() function to measure the time, that depends on the specific use case:import time
# Start timer
start_time = time.perf_counter()
# Code to be timed
# ...
# End timer
end_time = time.perf_counter()
# Calculate elapsed time
elapsed_time = end_time - start_time
print("Elapsed time: ", elapsed_time)
Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.
