From 9920d53927bbee22b8121b49e51196e555f7018a Mon Sep 17 00:00:00 2001 From: naglis <827324+naglis@users.noreply.github.com> Date: Thu, 14 Dec 2023 07:42:07 +0200 Subject: [PATCH 1/2] Add `@functools.wraps` on `timing` decorator Without it, the name/docstring of the wrapped function is lost. --- cozy/architecture/profiler.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cozy/architecture/profiler.py b/cozy/architecture/profiler.py index d5a84e8b..fd95093b 100644 --- a/cozy/architecture/profiler.py +++ b/cozy/architecture/profiler.py @@ -1,9 +1,11 @@ import time import logging +import functools log = logging.getLogger("timing") def timing(f): + @functools.wraps(f) def wrap(*args): time1 = time.time() ret = f(*args) From bb9d1b6ae6f4d72fca59774ca200ed76644892be Mon Sep 17 00:00:00 2001 From: naglis <827324+naglis@users.noreply.github.com> Date: Thu, 14 Dec 2023 07:50:35 +0200 Subject: [PATCH 2/2] Use `time.perf_counter()` for timing `time.time()` can return a lower value than a previous call if the system clock has been set back between the two calls. --- cozy/architecture/profiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cozy/architecture/profiler.py b/cozy/architecture/profiler.py index fd95093b..10a7009b 100644 --- a/cozy/architecture/profiler.py +++ b/cozy/architecture/profiler.py @@ -7,9 +7,9 @@ def timing(f): @functools.wraps(f) def wrap(*args): - time1 = time.time() + time1 = time.perf_counter() ret = f(*args) - time2 = time.time() + time2 = time.perf_counter() log.info('{:s} function took {:.3f} ms'.format(f.__name__, (time2-time1)*1000.0)) return ret