Categories
Python Answers

How to measure elapsed time in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *