Add `Profile()` profiler (#4587)
Browse files* Add `Profile()` profiler
* CamelCase Timeout
- utils/general.py +11 -2
utils/general.py
CHANGED
@@ -39,8 +39,17 @@ cv2.setNumThreads(0) # prevent OpenCV from multithreading (incompatible with Py
|
|
39 |
os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads
|
40 |
|
41 |
|
42 |
-
class
|
43 |
-
# Usage: @
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True):
|
45 |
self.seconds = int(seconds)
|
46 |
self.timeout_message = timeout_msg
|
|
|
39 |
os.environ['NUMEXPR_MAX_THREADS'] = str(min(os.cpu_count(), 8)) # NumExpr max threads
|
40 |
|
41 |
|
42 |
+
class Profile(contextlib.ContextDecorator):
|
43 |
+
# Usage: @Profile() decorator or 'with Profile():' context manager
|
44 |
+
def __enter__(self):
|
45 |
+
self.start = time.time()
|
46 |
+
|
47 |
+
def __exit__(self, type, value, traceback):
|
48 |
+
print(f'Profile results: {time.time() - self.start:.5f}s')
|
49 |
+
|
50 |
+
|
51 |
+
class Timeout(contextlib.ContextDecorator):
|
52 |
+
# Usage: @Timeout(seconds) decorator or 'with Timeout(seconds):' context manager
|
53 |
def __init__(self, seconds, *, timeout_msg='', suppress_timeout_errors=True):
|
54 |
self.seconds = int(seconds)
|
55 |
self.timeout_message = timeout_msg
|