From f4a8d860a52da68083df5cdd8910d9656191d930 Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 29 Aug 2021 13:42:33 +0200 Subject: [PATCH 1/2] Add `Profile()` profiler --- utils/general.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/utils/general.py b/utils/general.py index 16244903575a..8b559bbd9382 100755 --- a/utils/general.py +++ b/utils/general.py @@ -39,6 +39,15 @@ os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads +class Profile(contextlib.ContextDecorator): + # Usage: @Profile() decorator or 'with Profile():' context manager + def __enter__(self): + self.start = time.time() + + def __exit__(self, type, value, traceback): + print(f'Profile results: {time.time() - self.start:.5f}s') + + class timeout(contextlib.ContextDecorator): # Usage: @timeout(seconds) decorator or 'with timeout(seconds):' context manager def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True): From 782c1a62560434eef5fa488f3255051f921be1ce Mon Sep 17 00:00:00 2001 From: Glenn Jocher Date: Sun, 29 Aug 2021 13:45:46 +0200 Subject: [PATCH 2/2] CamelCase Timeout --- utils/general.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/utils/general.py b/utils/general.py index 8b559bbd9382..c74d8bb299de 100755 --- a/utils/general.py +++ b/utils/general.py @@ -48,8 +48,8 @@ def __exit__(self, type, value, traceback): print(f'Profile results: {time.time() - self.start:.5f}s') -class timeout(contextlib.ContextDecorator): - # Usage: @timeout(seconds) decorator or 'with timeout(seconds):' context manager +class Timeout(contextlib.ContextDecorator): + # Usage: @Timeout(seconds) decorator or 'with Timeout(seconds):' context manager def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True): self.seconds = int(seconds) self.timeout_message = timeout_msg