timer#
- class timer(label=None, auto=False, start=True, unit='auto', verbose=None, **kwargs)[source]#
Bases:
object
Simple timer class. Note:
sc.timer()
andsc.Timer()
are aliases.This wraps
sc.tic()
andsc.toc()
with the formatting arguments and the start time (at construction).Use this in a
with
block to automatically print elapsed time when the block finishes.By default, output is displayed in seconds. You can change this with the
unit
argument, which can be a string or a float:‘hr’ or 3600
‘min’ or 60
‘s’ or 1 (default)
‘ms’ or 1e-3
‘us’ or 1e-6
‘ns’ or 1e-9
‘auto’ to choose an appropriate unit
- Parameters:
label (str) – label identifying this timer
auto (bool) – whether to automatically increment the label
start (bool) – whether to start timing from object creation (else, call
timer.tic()
explicitly)unit (str/float) – the unit of time to display; see options above
verbose (bool) – whether to print output on each timing
Example making repeated calls to the same timer, using
auto
to keep track:>>> T = sc.timer(auto=True) >>> T.toc() (0): 2.63 s >>> T.toc() (1): 5.00 s
Example wrapping code using with-as:
>>> with sc.timer('mylabel'): >>> sc.timedsleep(0.5)
Example using a timer to collect data, using
timer.tt()
as an alias forsc.toctic()
to reset the time:T = sc.timer(doprint=False) for key in 'abcde': sc.timedsleep(pl.rand()) T.tt(key) print(T.timings)
Implementation based on https://preshing.com/20110924/timing-your-code-using-pythons-with-statement/
New in version 1.3.0:sc.timer()
alias, and allowing the label as first argument.New in version 1.3.2:toc()
passes label correctly;tt()
method;auto
argumentNew in version 2.0.0:plot()
method;total()
method;indivtimings
andcumtimings
propertiesNew in version 2.1.0:total
as property instead of method; updated repr; added disp() methodNew in version 3.0.0:unit
argument;verbose
argument;sum, min, max, mean, std
methods;rawtimings
propertyAttributes
Compute the cumulative time for each timing
Compute the individual time between each timing
Return an array of timings
Calculate total time
Methods
- property total#
Calculate total time
- tt(*args, **kwargs)[source]#
Alias for
sc.toctic()
- tto(*args, output=True, **kwargs)[source]#
Alias for
sc.toctic()
with output=True
- property rawtimings#
Return an array of timings
- property indivtimings#
Compute the individual time between each timing
- property cumtimings#
Compute the cumulative time for each timing
- sum()[source]#
Sum of timings; similar to
timer.total
New in version 3.0.0.