Skip to content

With statement

You can use the Stopwatch Class with the with statement.

python
from stopwatch import Stopwatch
from time import sleep

with Stopwatch() as my_stopwatch:
    sleep(3)
print(f'Time elapsed: {my_stopwatch}')  # Time elapsed: 3.00s
from stopwatch import Stopwatch
from time import sleep

with Stopwatch() as my_stopwatch:
    sleep(3)
print(f'Time elapsed: {my_stopwatch}')  # Time elapsed: 3.00s

When you are using the with statement, you can pass the print_report parameter to the Stopwatch class, to print the report at the end of execution.

python
from stopwatch import Stopwatch
from time import sleep

with Stopwatch(print_report=True):
    sleep(2)

# [__main__:<module>:4] ~ 2.00s
from stopwatch import Stopwatch
from time import sleep

with Stopwatch(print_report=True):
    sleep(2)

# [__main__:<module>:4] ~ 2.00s

Released under the MIT License.