Menu Close

How to measure elapsed time in Python?

Sometimes, we want to measure elapsed time in Python.

In this article, we’ll look at how to measure elapsed time in Python.

How to measure elapsed time in Python?

To measure elapsed time in Python, we can use the default_timer function in the time_it module.

For instance, we write

from timeit import default_timer as timer

start = timer()
# ...
end = timer()
print(end - start)

to import default_timer as timer.

And then we call it to get the start and end times.

Then we get the elapsed time in seconds between the time when the 2 lines is run by subtracting end by start.

The elapsed time is in seconds.

Conclusion

To measure elapsed time in Python, we can use the default_timer function in the time_it module.

Posted in Python, Python Answers