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 time module.

For instance, we write:

import time

start = time.time()
print("hello world")
end = time.time()
print(end - start)

We call time.time to return the current time as a Unix timestamp and assign the result to start and end.

And then we subtract end from start to get the elapsed time in seconds.

Conclusion

To measure elapsed time in Python, we can use the time 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 *