Python’s with statement, available since Python 2.5, does not seem to be widely used despite being very useful.
Here we describe how to use the
with statement to create a simple timer with the syntax:
>>> with TicToc(): ... some_slow_operations ... Elapsed time is 2.000073 seconds.
Readers familiar with MATLAB will recognize the similarity to the familiar timing mechanism:
> tic ; some_slow_operations; toc Elapsed time is 10.020349 seconds.