How to See How Long Your Python Script Took to Run
It's really helpful and fun to see how long a script took to run. It can inform you on how successful the script was in your productivity workflow, or you can use it to determine how effective your programming is, or figure out where the script breaks down in efficiency.
I started out with this snippet, which is a convenient way to track runtime since you just throw a start time at the top, end time at the bottom, and a couple more lines to print the difference in a human-readable way.
import time
start = time.time()
# DO SOMETHING
# CALC & PRINT RUNTIME
end = time.time()
print(end - start)
This way may not be the most precise measurement if you need something more accurate than seconds (although it's still really good!)
The .time() method uses the System Clock, but there's another method that uses the CPU instead: .perf_counter()
Using perf_counter()
import time
start=time.perf_counter()
# DO SOMETHING
# CALC & PRINT RUNTIME
end = time.perf_counter()
print(end - start)
The difference between the output of the .time() and the .perf_counter() calculations are pretty negligible in my opinion—but I also don't need nano-second granularity.
The general consensus is that .perf_counter() is probably the preferred method for this kind of stopwatch-style runtime tracking.
Here's an example of the output I got from running both of these methods on a script that simply slept for 2 seconds:
[.TIME METHOD]
2.0021018981933594
[.PERF_COUNTER METHOD]
2.0028419459999998
Both output the end - start
expression as floats. .perf_counter is simply more accurate at the smaller stuff.
Formatting the Output
This is my snippet for formatting the result of the difference between end
and start
times:
runtime = round((end - start), 2)
if runtime < 60:
print(f'Runtime: {runtime} seconds')
else:
print('Runtime: ' + str(round((runtime/60), 2)) + ' minutes')
Assuming you've used end
and start
as the variable names, this is an easy snippet to repeat regardless of your choice of .time or .perf_counter. Outputs:
[.TIME METHOD]
Runtime: 2.01 seconds
[.PERF_COUNTER METHOD]
Runtime: 2.0 seconds
If you have a longer-running script that takes minutes to run, it will output like this:
[.TIME METHOD]
Runtime: 1.03 minutes
[.PERF_COUNTER METHOD]
Runtime: 1.03 minutes
Hours and Alternative String Formatting
If you're really stretching that script into hours, you can modify the if statements like this:
runtime = end - start
if runtime < 60:
print(f'Runtime: {runtime:.2f} seconds')
elif runtime < 3600: # Less than one hour
minutes = runtime / 60
print(f'Runtime: {minutes:.2f} minutes')
else:
hours = runtime / 3600
print(f'Runtime: {hours:.2f} hours')
Which, given a runtime of 4700 seconds, would output something like this:
Runtime: 1.31 hours
I also switched the round() to just string formatted rounding in this final version to reduce visual clutter. Instead of rounding the runtime
prior to any calculations (like dividing by 60 or 3600), we combine f-strings and formatting to insert the time, still rounded.
The :
starts the formatting pattern, .2
tells it to use 2 decimal places, f
specifies that it should be a floating-point number.