Calculating a time from the starting event to the ending event is useful in real-time cases. In programming, you can calculate the code execution time for different scenarios. In this tutorial, we will see how to calculate elapsed time with different use cases.
What is elapsed time?
Elapsed time is the amount of time that passes from the start point to the endpoint. In simple terms, the elapsed time is how much time passed by from one point in time to another point in time.
How to Measure Python elapsed time
To measure the elapsed time between two points in Python, use the time.time() method.
- Set a start_time by assigning a time.time() value to a variable.
- Set a end_time by assigning time.time() value to a variable.
- Subtract the start_time from the end_time to get time elapsed in seconds.
The time.time() is a method of Time module that is used to get the time in seconds since the epoch.
import time start_time = time.time() print("PythonSolved") end_time = time.time() print(end_time - start_time)
In this example, we defined the first start_time as equal to time.time() then we printed PythonSolved in the console, and then we again a defined end_time variable whose value is time.time().
Then we subtract start_time from end_time and the remaining output time is the elapsed time between two data points.
The timeit.default_timer() is one of the important timeit module methods that returns the default time when executed. The default_timer() method delivers the best clock available on your platform and with respect to the version of Python automatically.
from timeit import default_timer as timer start = timer() end = timer() print(end - start)
In this example, we are using the default_timer() function which gives the accurate timing as per our Python version and platform. In my case, I am using Python 3.9 and Mac platform.
Using a specific method for Python3 only
The time.process_time() is the Python3 method that returns the value (it is in fractional seconds) of the sum of the system and user CPU time of the current process.
import time start = time.process_time() end = time.process_time() print(end - start)
The process_time() method does not include time elapsed during sleep because it is a process-wide calculation by definition. So if you want to measure the time elapsed that does not include sleep, then use the process_time() method.
In some cases, the process_time() function may not be appropriate to measure elapsed time. In that case, use the default_timer() method which gives you the accurate time elapsed.
Python elapsed time in seconds
The timedelta() is a built-in Python function that resides under the datetime library which is used for calculating differences in dates and also used for date manipulations. To calculate elapsed time in seconds, use the timedelta() function in Python.
import time from datetime import timedelta start_time = time.time() print("PythonSolved") print("PythonSolved") print("PythonSolved") print("PythonSolved") end_time = time.time() print(timedelta(seconds=end_time-start_time))
PythonSolved PythonSolved PythonSolved PythonSolved 0:00:00.000020
Here, we put some code between start_time and end_time and that’s why the elapsed time shows some seconds.
That’s it for elapsed time in Python.
Krunal Lathiya is an Information Technology Engineer. By profession, he is a web developer with knowledge of multiple back-end platforms including Python. Krunal has written many programming blogs which showcases his vast knowledge in this field.