How do I get time of a Python program's execution?

Better Stack Team
Updated on February 3, 2023

You can use the time module in Python to get the time of a program's execution. The time() function returns the current time in seconds since the epoch (the epoch is a predefined point in time, usually the beginning of the year 1970). To get the time at the start of the program, you can call time() before running the rest of the program. To get the time at the end of the program, you can call time() again after the program has finished running. The difference between the two times is the total execution time of the program.

For example:

import time

start_time = time.time()

# run program here

end_time = time.time()

execution_time = end_time - start_time
print("Execution Time: ", execution_time)
Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

We are hiring.

Software is our way of making the world a tiny bit better. We build tools for the makers of tomorrow.

Explore all positions →