Skip to content

Commit 8923b2e

Browse files
Abhishek_Dutta3Abhishek_Dutta3
authored andcommitted
added profiler with python decorator function
1 parent 33115ea commit 8923b2e

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

Python Decorators/app.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from profiler import profile
2+
from loguru import logger
3+
import numpy as np
4+
from random import randint
5+
import math
6+
import time
7+
8+
9+
@profile
10+
def some_task():
11+
logger.info("Starting Long Running Task")
12+
result = np.empty(5, dtype=np.float64)
13+
a = np.random.rand(10 ** 6)
14+
b = np.random.rand(10 ** 6)
15+
for i in range(len(result)):
16+
result[i] = math.exp(2.1 * a[i] + 3.2 * b[i])
17+
time.sleep(randint(1, 2))
18+
19+
20+
if __name__ == "__main__":
21+
some_task()

Python Decorators/profiler.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from functools import wraps
2+
import tracemalloc
3+
import time
4+
5+
6+
def profile(f):
7+
"""
8+
## Decorator to profile memory usage by a function
9+
"""
10+
11+
def memory_humanize(num, suffix="B"):
12+
"""
13+
## Convert bytes to human readable format
14+
"""
15+
for unit in ["", "K", "M", "G", "T", "P", "E", "Z"]:
16+
if abs(num) < 1024.0:
17+
return f"{num:3.1f}{unit}{suffix}"
18+
num /= 1024.0
19+
return f"{num:.1f}Yi{suffix}"
20+
21+
def time_humanize(seconds):
22+
"""
23+
## Convert seconds to human readable format
24+
"""
25+
h = int(seconds / 3600)
26+
m = int((seconds % 3600) / 60)
27+
s = int(seconds % 60)
28+
return f"{h}h {m}m {s}s"
29+
30+
@wraps(f)
31+
def decorator(*args, **kwargs):
32+
tracemalloc.start()
33+
start_time = time.time()
34+
res = f(*args, **kwargs)
35+
current, peak = tracemalloc.get_traced_memory()
36+
print(f"profiler.mem.current: {memory_humanize(current) }")
37+
print(f"profiler.mem.peak: {memory_humanize(peak) }")
38+
tracemalloc.stop()
39+
end_time = time.time()
40+
print(f"profiler.duration: {time_humanize(end_time-start_time)}")
41+
return res
42+
43+
return decorator

Python Decorators/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
loguru
2+
numpy # for creating sample load bearing function [dev deps]

0 commit comments

Comments
 (0)