How do I measure elapsed time in Python?
To monitor execution time to the highest precision, Python's time
module and its rich time.perf_counter()
function come in handy. Before starting your code, record the start time, post execution, calculate the difference between the end and start time to fetch the time spent.
This results in a high precision measurement of elapsed time, making it perfect for performance testing. It spares the time during sleep and also considers background processes.
Deeper Understanding: Timing verbosity
When you want the focusing solely on the CPU cycles, namely ignoring I/O operations API like sleep, time.process_time()
becomes your friend. This way you get a better picture of pure computational time:
Contrarily, it's worth noting that time.clock()
has become an artifact of the past, and is obsolete!
When precision is not the primary focus, yet timing is yet required, time.time()
falls into the grace. It becomes a choice for timing operations where the significant figures of time.perf_counter()
are non-impactful:
timeit.default_timer()
: A Helper for Accurate Code snippet Timing
In need of high accuracy timing for bite-sized code snippets, an essential tool in Python developers' kit is timeit.default_timer()
. It auto collects the best precise timer available. No more guesswork for you!
In Python 3.3 or later, timeit.default_timer()
defaults to time.perf_counter()
, aiding precise timings straight out of the box.
Beautiful Representation of Elapsed Time
When readability is utmost, making elapsed time human-friendly becomes essential. You may use datetime.timedelta
to please the human eyes and brain:
This results in time displayed as hours:minutes:seconds.microseconds
, much more digestible.
Crucial Aspects of Timing
Keep timing your code, remembering to exclude print statements and other I/O operations which can introduce variance in measurements:
For single-run time measurements, default_timer
usually surpasses multiple runs with timeit.timeit()
in accuracy. However, reliable repetitive sampling with timeit
can lead to more statistically valid benchmarks.
Practical Scenarios and Timing Pitfalls
Systematic Timing vs Pure Computing Time
Depending on what you're monitoring:
- System-wide timing inclusive of I/O waiting time is grasped better with
time.perf_counter()
as it provides real-time elapsed measurement. - For extracting pure computational time, devoid of sleep time or other interference,
time.process_time()
is more accurate.
Timing Loops and Recurring Tasks
In scenarios where you're timing loops or iterative tasks, you may want to calculate the cumulative time or time for individual iterations:
Challenges in Optimization
While working for speeding up the code:
- Resources like scikit-learn's "How to optimize for speed" can be a handy guide.
- Profiling tools such as
cProfile
might be worth consideration to pinpoint bottlenecks. - Reminisce the tradeoff between efforts pumped into optimization against maintainability. Often the most significant speedup originates from algorithmic tweaks rather than micro-optimizations.
Was this article helpful?