Stopwatch
A simple stopwatch for measuring code performance.
Installing
To install the library, you can just run the following command:
# Linux/macOS
python3 -m pip install python-stopwatch
# Windows
py -3 -m pip install python-stopwatch
To install the development version, do the following:
git clone https://github.com/jonghwanhyeon/python-stopwatch
cd python-stopwatch
python3 -m pip install .
Examples
import time
from stopwatch import Stopwatch, profile
stopwatch = Stopwatch()
stopwatch.start()
time.sleep(3.0)
stopwatch.stop()
print(stopwatch.elapsed)
with Stopwatch(name='outer') as outer_stopwatch:
with Stopwatch(name='inner') as inner_stopwatch:
for i in range(5):
with inner_stopwatch.lap():
time.sleep(i / 10)
print(inner_stopwatch.elapsed)
print(inner_stopwatch.laps)
print(outer_stopwatch.report())
print(inner_stopwatch.report())
@profile
def wait_for(ts):
if not ts:
return
time.sleep(ts[0])
wait_for(ts[1:])
wait_for([0.1, 0.2, 0.3, 0.4, 0.5])
@profile(name='wait for ts')
def wait_for(ts):
if not ts:
return
time.sleep(ts[0])
wait_for(ts[1:])
wait_for([0.1, 0.2, 0.3, 0.4, 0.5])
@profile(name='wait for ts', report_every=2)
def wait_for(ts):
if not ts:
return
time.sleep(ts[0])
wait_for(ts[1:])
wait_for([0.1, 0.2, 0.3, 0.4, 0.5])
@profile(name='wait for ts', report_every=None)
def wait_for(ts):
if not ts:
return
time.sleep(ts[0])
wait_for(ts[1:])
wait_for([0.1, 0.2, 0.3, 0.4, 0.5])
with stopwatch():
for i in range(5):
time.sleep(i / 10)
with stopwatch('with message'):
for i in range(5):
time.sleep(i / 10)