timer#
- class timer(label=None, auto=False, start=True, unit='auto', verbose=None, **kwargs)[source]#
Bases:
objectSimple 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
withblock to automatically print elapsed time when the block finishes.By default, output is displayed in seconds. You can change this with the
unitargument, 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
autoto 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(verbose=False) for key in 'abcde': sc.timedsleep(np.random.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 argumentNew in version 1.3.2:toc()passes label correctly;tt()method;autoargumentNew in version 2.0.0:plot()method;total()method;indivtimingsandcumtimingspropertiesNew in version 2.1.0:totalas property instead of method; updated repr; added disp() methodNew in version 3.0.0:unitargument;verboseargument;sum, min, max, mean, stdmethods;rawtimingspropertyNew in version 3.1.0: Timers can be combined by addition, includingsum()New in version 3.1.5:T.timingsis now ansc.objdict()instead of ansc.odict()New in version 3.2.2:sc.timer()can be used as a function decoratorNew in version 3.2.5:.stringattribute (e.g. ‘3.25 s’)Attributes
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.totalNew in version 3.0.0.