Time Python Script File by PowerShell Commands Measure-Command and Python cProfile Module

Jul. 23, 2024

In Subsection “14.13 Profiling and Timing Your Program” of David Beazley and Brian K. Jones’s book Python Cookbook1, some methods are provided to get Python code execution time. Before introducing how to time code snippets, authors firstly give two ways of timing whole Python script.

The first one is using Unix time command (the similar command on Windows PowerShell is Measure-Command2 as will be showed), and the second is by Python cProfile module3.


To test both methods, I create a .py file example.py:

1
2
3
4
# example.py

for i in range(int(1E9)): 
    i += 1

(1) PowerShell Measure-Command command

1
Measure-Command {python example.py}
1
2
3
4
5
6
7
8
9
10
11
Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 33
Milliseconds      : 347
Ticks             : 333478729
TotalDays         : 0.000385970751157407
TotalHours        : 0.00926329802777778
TotalMinutes      : 0.555797881666667
TotalSeconds      : 33.3478729
TotalMilliseconds : 33347.8729

(2) Python cProfile module

1
python -m cProfile example.py
1
2
3
4
5
6
7
8
         3 function calls in 49.644 seconds

   Ordered by: cumulative time

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000   49.644   49.644 {built-in method builtins.exec}
        1   49.644   49.644   49.644   49.644 example.py:1(<module>)
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}


As can be seen, both two timing methods only execute script file one time, so they’re time-saving, but meanwhile their test errors are relatively large. Therefore, they’re naturally more suitable for timing a whole script file rather than code snippets (actually above example.py is more like code snippet, and hence not that appropriate for illustrating both methods).


References