How to Measure elapsed time between Two Points in Python

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.

  1. Set a start_time by assigning a time.time() value to a variable.
  2. Set a end_time by assigning time.time() value to a variable.
  3. 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)

Output

PythonSolved
1.6927719116210938e-05

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.

timeit.default_timer()

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)

Output

1.2500000000359446e-07

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)

Output

2.9999999999960614e-06

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))

Output

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.

More posts

Python print float

Python floor float

Leave a Comment