jztools.profiling#
Thread-safe profiling utilities to measure time, counts and rates.
Classes
|
Similar to |
|
|
|
Count that also keeps track of rate. |
|
Keeps track of the min, max, and avg updates. |
|
Context manager that accumulates wall time and keeps track of statistics (min,max,avg,total) of each update. Updates happen with each call to in-place adition or context manager exit. |
|
Times a function or code context and prints the time. |
- class jztools.profiling.Scalar(decay=0.6, format_string=None, lock=None)#
Bases:
objectKeeps track of the min, max, and avg updates.
sc = Scalar(decay=1.0) sc.update(1.0) #min, avg, max = 1.0, 1.0, 1.0 sc.update(0.5) #min, avg, max = 0.5, 0.75, 1.0 sc.update(1.5) #min, avg, max = 0.5, 1.5, 1.5
- Parameters:
decay – Decay to use in weighted average.
- max = -inf#
Maximum value of updates.
- min = inf#
Minimum value of updates.
- updates = 0#
Total number of updates.
- avg = 0.0#
Running average of update values.
- class jztools.profiling.Count(decay=0.6, format_string=None, lock=None)#
Bases:
ScalarSimilar to
Scalarbut also accumulates updates and exposes a += operator. Values can be real numbers and not only integers. In effect, the class accumulates whatever values are passed to it.sc = Count(decay=1.0) # the update() method and += operator are equivalent sc += 1.0 # min, avg, max, total = 1.0, 1.0, 1.0, 1.0 sc += 0.5 # min, avg, max, total = 0.5, 0.75, 1.0, 1.5 sc += 1.5 # min, avg, max, total = 0.5, 1.5, 1.5, 3.0
- Parameters:
decay – Decay to use in weighted average.
- total = 0.0#
Sum of all updates.
- class jztools.profiling.time_and_print(msg=None, bang='**********', signal_enter=False)#
Bases:
AbstractContextManagerTimes a function or code context and prints the time.
As context manager: with time_and_print():
…
As decorator: @time_and_print() def function(): pass
- __call__(function)#
Call self as a function.
- class jztools.profiling.Time(*args, **kwargs)#
Bases:
Count,AbstractContextManagerContext manager that accumulates wall time and keeps track of statistics (min,max,avg,total) of each update. Updates happen with each call to in-place adition or context manager exit.
Attempting to enter an already-entered context manager will raise an error.
t = Time() # First update. with t: sleep(1) # t.elapsed == 1 # No update sleep(1) # t.elapsed == 1 # Second update. with t: sleep(1) # t.elapsed == 2 # Third update. t += 2 # t.elapsed == 4 # with t: with t: # Raises an error. pass
- Parameters:
decay – Decay to use in weighted average.
- property elapsed#
Similar to self.total, except that in can be called inside a context and will include partial context time. Outside a context, same as total.
- class jztools.profiling.Rate(*args, update_interval=1.0, **kwargs)#
Bases:
Scalar,AbstractContextManagerCount that also keeps track of rate. Counts can be real numbers and not just integers. Rates are computed every “update_interval” seconds using a spawned thread and tracked using a running average with decay (as any
Scalar()).# r = Rate() with r: time.sleep(2.0) r+=3 # r.total ~= 3/2
Besides the arguments of
Scalar, also accepts- Parameters:
update_interval – Determines how often the spawned thread will update rate.