Skip to content

Commit e27833e

Browse files
authored
Create performance.py
1 parent 08cd146 commit e27833e

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

performance.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import time
2+
import logging
3+
import tracemalloc
4+
from functools import wraps
5+
6+
7+
def track_performance(func):
8+
"""
9+
Decorator to track runtime and memory usage of functions.
10+
"""
11+
12+
@wraps(func)
13+
def wrapper(*args, **kwargs):
14+
15+
logging.info(f"Starting: {func.__name__}")
16+
17+
# Start timing
18+
start_time = time.time()
19+
20+
# Start memory tracking
21+
tracemalloc.start()
22+
23+
result = func(*args, **kwargs)
24+
25+
current, peak = tracemalloc.get_traced_memory()
26+
tracemalloc.stop()
27+
28+
elapsed = time.time() - start_time
29+
30+
logging.info(
31+
f"Completed: {func.__name__} | "
32+
f"Runtime: {elapsed:.2f}s | "
33+
f"Peak Memory: {peak / 10**6:.2f} MB"
34+
)
35+
36+
return result
37+
38+
return wrapper

0 commit comments

Comments
 (0)