mbrs.timer module#

Timer module including some useful global objects.

Example

>>> from mbrs import timer
>>> for hyps, src in zip(hypotheses, sources):
...     with timer.measure("encode/hypotheses") as t:
...         h = metric.encode(hyps)
...     t.set_delta_ncalls(len(hyps))
...     with timer.measure("encode/source"):
...         s = metric.encode([src])
...     with timer.measure("score"):
...         scores = metric.score(h, s)
>>> res = timer.aggregate().result()  # return the result table
class mbrs.timer.ProfileTree(elapsed_time: 'float' = -1.0, ncalls: 'int' = -1, children: 'dict[str, ProfileTree]' = <factory>)[source]#

Bases: object

aggregate()[source]#
classmethod build(timers: StopwatchDict, separetor: str = '/')[source]#
children: dict[str, ProfileTree]#
elapsed_time: float = -1.0#
property is_leaf: bool#

Return whether the node is leaf or not.

ncalls: int = -1#
result(nsentences: int = -1) list[dict[str, str | int | float]][source]#
class mbrs.timer.Stopwatch[source]#

Bases: object

Stopwatch class to measure the elapsed time.

Example

>>> timer = Stopwatch()
>>> for i in range(10):
...     with timer():
...         time.sleep(1)
>>> print(f"{timer.elapsed_time:.3f}")
10.000
>>> timer = Stopwatch()
>>> for i in range(10):
...     with timer() as t:
...         time.sleep(2)
...         t.set_delta_ncalls(2)
>>> print(f"{timer.elapsed_time:.3f}")
20.000
>>> print(f"{timer.ncalls}")
20
property elpased_time: float#

Return the total elapsed time.

property ncalls: int#

Return the number of calls.

reset() None[source]#

Reset the stopwatch.

set_delta_ncalls(delta: int = 1)[source]#

Set delta for counting the number of calls.

class mbrs.timer.StopwatchDict[source]#

Bases: defaultdict[str, Stopwatch]

A dictionary of the Stopwatch class.

Example

>>> timers = StopwatchDict()
>>> for i in range(10):
...     with timers("A"):
...         time.sleep(1)
>>> for i in range(3):
...     with timers("B"):
...         time.sleep(1)
>>> print(f"{timers.total}")
{"A": 10.000, "B": 3.000}
property elapsed_time: dict[str, float]#

Return the total elapsed time.

property ncalls: dict[str, int]#

Return the number of calls.

reset() None[source]#

Reset all stopwatches.

mbrs.timer.aggregate() ProfileTree[source]#

Aggregate the timers.

Returns:

The root of the profile tree.

Return type:

ProfileTree