Source code for tcutility.timer

import atexit
import functools
from time import perf_counter
from types import FunctionType
from typing import Dict, Sequence, Union

import listfunc
import numpy as np

import tcutility.log as log

"""
Implements a decorator and context manager that records and stores the number of times a function
has been called and also the amount of time taken in total/per call
"""
times = {}
exec_start = perf_counter()

enabled: bool = True

timer_level: int = 10


[docs] class timer: """ The main timer class. It acts both as a context-manager and decorator. """ def __init__(self, name: str = "", level: int = 20): """ Args: name: a custom name to give to the function. level: the level at which to record timings. """ self.level = level if isinstance(name, FunctionType): if self.level < timer_level: return name self.function = name times.setdefault(name.__qualname__, {"calls": 0, "timings": []}) elif name is not None: self.name = name times.setdefault(self.name, {"calls": 0, "timings": []}) def __enter__(self): self.start = perf_counter() return self def __exit__(self, *args, **kwargs): times[self.name]["calls"] += 1 times[self.name]["timings"].append(perf_counter() - self.start) def __call__(self, *args, **kwargs): if not hasattr(self, "function") and isinstance(args[0], FunctionType): if self.level < timer_level: return args[0] self.function = args[0] times.setdefault(self.function.__qualname__, {"calls": 0, "timings": []}) return self times.setdefault(self.function.__qualname__, {"calls": 0, "timings": []}) if enabled and __debug__: start = perf_counter() ret = self.function(*args, **kwargs) times[self.function.__qualname__]["calls"] += 1 times[self.function.__qualname__]["timings"].append(perf_counter() - start) return ret else: return self.function(*args, **kwargs) def __get__(self, obj, objtype): return functools.partial(self.__call__, obj)
# this makes sure that the timings are printed when Python quits running atexit.register(print_timings) if __name__ == "__main__": import time @timer() def a(b): time.sleep(b) return b @timer(level=10) def b(b): time.sleep(b) return b class A: @timer() def a(self): ... print(a) print(b) a(0.3) a(0.3) b(0.2) b(0.2) b(0.2) a = A() a.a()