module emote.utils.timed_call

Simple block-based timers using Welford's Online Algorithm to approximate mean and variance.

Usage:


timer = TimedBlock()

for _ in range(10):
    with timer():
        sleep(1)

print(time.sleep())

# (1.000013, 1.3e-5)

## Classes

### `class StatisticsAccumulator(ABC):`

<div style="padding-left: 20px;">

Interface for a statistics integrator.

#### Methods

```python
def add(self, value) -> None

Add the value to the running statistics.

Arguments:

  • value(float): the sample to integrate
def current(self) -> Tuple[float, float]

Returns the statistics of the observed samples so far.

Returns:

  • a tuple (mean, variance)

class WelfordAccumulator(StatisticsAccumulator):

Implements Welford's Online Algorithm for single-pass variance and mean.

Fields

  • count: int = 0

  • mean: float = 0.0

  • differences: float = 0.0

Methods

def add(self, value) -> None

Add the value to the running statistics.

Arguments:

  • value(float): the sample to integrate
def current(self) -> Tuple[float, float]

Returns the current values of the Welford algorithm.

Returns:

  • a tuple (mean, variance)

class MovingWindowAccumulator(StatisticsAccumulator):

Fields

  • values: deque = field(default_factory=lambda : deque(maxlen=100))

Methods

def add(self, value) -> None

Add the value to the running statistics.

Arguments:

  • value(float): the sample to integrate
def current(self) -> Tuple[float, float]

Returns the current statistics.

Returns:

  • a tuple (mean, variance)

class TimedBlock:

Used to track the performance statistics of a block of code, in terms of execution time.

Methods

def __init__(self, tracker_type) -> None

Create a new timed block instance.

Arguments:

  • tracker_type(Type[StatisticsAccumulator]): The statistics integrator to use. Defaults to to MovingWindowStats
def mean(self) -> float

Retrieve the mean execution time.

def var(self) -> None

Retrieve the variance of the execution time.

def stats(self) -> None

Retrieve the mean and the variance of execution time.

class BlockTimers:

Methods

def __init__(self, tracker_type) -> None
def scope(self, name) -> TimedBlock
def stats(self) -> None