Skip to content

Timers

Clock = Nullary[int] module-attribute

The type representing clocks which return time in nanoseconds.

Elapsed

Represents elapsed time, in nanoseconds and human-readable format.

Source code in aoc/timers.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@final
@frozen()
class Elapsed:
    """Represents elapsed time, in nanoseconds and human-readable format."""

    nanoseconds: int = field()
    """The time elapsed, in nanoseconds."""

    rounding: int = field(default=DEFAULT_ROUNDING)
    """The rounding to use when converting to human-readable format."""

    string: str = field(init=False)
    """The time elapsed, in human-readable format."""

    @string.default
    def default_string(self) -> str:
        nanoseconds = self.nanoseconds

        for name, factor in FACTORS:
            if nanoseconds > factor:
                return str(round(nanoseconds / factor, self.rounding)) + name

        return INSTANT

    def __str__(self) -> str:
        return self.string

nanoseconds: int = field() class-attribute instance-attribute

The time elapsed, in nanoseconds.

rounding: int = field(default=DEFAULT_ROUNDING) class-attribute instance-attribute

The rounding to use when converting to human-readable format.

string: str = field(init=False) class-attribute instance-attribute

The time elapsed, in human-readable format.

Timer

Represents timers.

Source code in aoc/timers.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
@final
@frozen()
class Timer:
    """Represents timers."""

    clock: Clock = field(default=default_clock)
    """The clock to use."""

    created: int = field(init=False)
    """The creation time of the timer."""

    @created.default
    def default_created(self) -> int:
        return self.clock()

    def elapsed(self, rounding: int = DEFAULT_ROUNDING) -> Elapsed:
        """Returns the time elapsed since the creation of this timer.

        Returns:
            The time elapsed since the creation of this timer.
        """
        return Elapsed(self.clock() - self.created, rounding)

    def reset(self) -> Self:
        """Creates and returns a new timer of the same type with the same clock.

        Returns:
            The timer created.
        """
        return type(self)(self.clock)

clock: Clock = field(default=default_clock) class-attribute instance-attribute

The clock to use.

created: int = field(init=False) class-attribute instance-attribute

The creation time of the timer.

elapsed(rounding: int = DEFAULT_ROUNDING) -> Elapsed

Returns the time elapsed since the creation of this timer.

Returns:

Type Description
Elapsed

The time elapsed since the creation of this timer.

Source code in aoc/timers.py
62
63
64
65
66
67
68
def elapsed(self, rounding: int = DEFAULT_ROUNDING) -> Elapsed:
    """Returns the time elapsed since the creation of this timer.

    Returns:
        The time elapsed since the creation of this timer.
    """
    return Elapsed(self.clock() - self.created, rounding)

reset() -> Self

Creates and returns a new timer of the same type with the same clock.

Returns:

Type Description
Self

The timer created.

Source code in aoc/timers.py
70
71
72
73
74
75
76
def reset(self) -> Self:
    """Creates and returns a new timer of the same type with the same clock.

    Returns:
        The timer created.
    """
    return type(self)(self.clock)

now(clock: Clock = default_clock) -> Timer

Creates a new timer with the given (or default) clock.

Parameters:

Name Type Description Default
clock Clock

The clock to use.

perf_counter_ns

Returns:

Type Description
Timer

The timer created.

Source code in aoc/timers.py
79
80
81
82
83
84
85
86
87
88
def now(clock: Clock = default_clock) -> Timer:
    """Creates a new timer with the given (or default) clock.

    Arguments:
        clock: The clock to use.

    Returns:
        The timer created.
    """
    return Timer(clock)