Skip to content

INFR: write a Timer context manager #782

@mmcky

Description

@mmcky

There are a lot of code patterns that use a timer such as

start_time = time()
freq = compute_freq(params, key).block_until_ready()
end_time = time()
print(f"Elapsed time: {(end_time - start_time) * 1000:.6f} ms")

We could modernise the QuantEcon timer by writing a simple context manager such as

import time


class Timer:
    def __enter__(self):
        self._enter_time = time.time()

    def __exit__(self, *exc_args):
        self._exit_time = time.time()
        print(f"{self._exit_time - self._enter_time:.2f} seconds elapsed")


if __name__ == "__main__":
    with Timer():
        # ...

that would print the time elapsed using a context manager.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions