Stopwatch
A simple stopwatch for measuring code performance.
Installing
To install the library, you can just run the following command:
$ python3 -m pip install python-stopwatch
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())
import time
from stopwatch import profile
@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])
import time
from stopwatch import profile
@profile("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])
import time
from stopwatch import profile
@profile("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])
import time
from stopwatch import profile
@profile("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])
import time
from stopwatch import profile
@profile(
"wait for ts",
format="<bold>[<blue>{module}</blue>:<green>{name}</green>]</bold> {statistics:hits, mean, median, stdev}",
)
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])
import time
from stopwatch import stopwatch
with stopwatch():
for i in range(5):
time.sleep(i / 10)
with stopwatch("with message"):
for i in range(5):
time.sleep(i / 10)