Skip to content

openavmkit.utilities.timing

TimingData

TimingData()

A simple timing class for quick & dirty performance profiling.

This class holds multiple internal "stopwatches" which can each keep track of their own running time. Just instantiate a TimingData and call start() and stop() with a desired key.

Attributes:

Name Type Description
results dict[str:float]

Raw timing results

Source code in openavmkit/utilities/timing.py
18
19
20
def __init__(self):
    self._data = {}
    self.results = {}

get

get(key)

Get the running time of the indicated stopwatch

Parameters:

Name Type Description Default
key str

The stopwatch to get timing data for

required

Returns:

Type Description
float

How long the indicated stopwatch has run, if it exists

Source code in openavmkit/utilities/timing.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
def get(self, key):
    """Get the running time of the indicated stopwatch

    Parameters
    ----------
    key : str
        The stopwatch to get timing data for

    Returns
    -------
    float
        How long the indicated stopwatch has run, if it exists
    """
    return self.results.get(key)

print

print()

Print the value of all stopwatches

Source code in openavmkit/utilities/timing.py
70
71
72
73
74
75
76
77
def print(self):
    """Print the value of all stopwatches"""
    value = ""
    for key in self.results:
        if value != "":
            value += "\n"
        value += f"{key}: {self.results[key]:.2f} seconds"
    return value

start

start(key)

Start a named stopwatch

Parameters:

Name Type Description Default
key str

Name of the stopwatch to begin timing, or resume timing if it already exists.

required
Source code in openavmkit/utilities/timing.py
22
23
24
25
26
27
28
29
30
31
32
33
def start(self, key):
    """Start a named stopwatch

    Parameters
    ----------
    key : str
        Name of the stopwatch to begin timing, or resume timing if it already exists.
    """
    if key in self.results:
        self._data[key] = time.time() - self.results[key]
    else:
        self._data[key] = time.time()

stop

stop(key)

Stop a named stopwatch

Parameters:

Name Type Description Default
key str

If this stopwatch has been started before, stops it if running and calculates how long it has run for.

required

Returns:

Type Description
float

How long the stopwatch has been running for

Source code in openavmkit/utilities/timing.py
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def stop(self, key):
    """Stop a named stopwatch

    Parameters
    ----------
    key : str
        If this stopwatch has been started before, stops it if running and calculates how long it has run for.

    Returns
    -------
    float
        How long the stopwatch has been running for
    """
    if key in self._data:
        result = time.time() - self._data[key]
        self.results[key] = result
        return result
    else:
        return -1