Skip to content

.start(), .elapsed(), and .log()

Jacob Morris edited this page Jun 6, 2019 · 1 revision

.decorate() and .time_it() provide ways to measure the execution time of functions and strings, but there are cases when the code needing timed is a series of statements that aren't wrapped in a function. That is what .start() and .elapsed() on StaticTimer and .start() and .log() on Timer are for. For both of them, .start() MUST be called before .log() or .elasped(). However, there is a slight workaround if Timer(start=True) is used when creating the non-static timer.

After .start() has been called, .log() or .elapsed() can be used to return/output or store the amount of time that has passed since the call to .start(). reset=True can be passed into either .log() or .elapsed() to automatically call .start() after the elasped time has been determined.

Note: A split must be created with Timer before .log() can be called. This can be done by calling .split() beforehand or during timer creation with Timer(split=True).

StaticTimer.start()
sleep(1)
StaticTimer.elapsed()
# 1000.09339 ms - Elapsed                                    [runs=  1, iterations=  1]   
sleep(1)
StaticTimer.elapsed(reset=True)
# 2000.37642 ms - Elapsed                                    [runs=  1, iterations=  1] 
sleep(1)
StaticTimer.elapsed()
# 1000.89325 ms - Elapsed                                    [runs=  1, iterations=  1]     
timer = Timer(start=True, split=True)
fib.__wrapped__(10)  # from fibonacci code in other examples, call .__wrapped__ to skip timing code 
timer.log(10, label="fib")
timer.output()
# Split:
#        0.03486 ms - fib(10)                                    [runs=  2, iterations=  1]    
Clone this wiki locally