Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions Week04/decorators_firat_adar_dal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import time
import tracemalloc

def performance(fn):
"""A decorator to measure the performance (time and memory usage) of a function."""
# Static variables for performance tracking
if not hasattr(performance, "counter"):
performance.counter = 0
performance.total_time = 0
performance.total_mem = 0

def wrapper(*args, **kwargs):
# Increment the call counter
performance.counter += 1

# Start tracking memory and time
tracemalloc.start()
start_time = time.time()

# Execute the decorated function
result = fn(*args, **kwargs)

# Stop tracking memory and calculate elapsed time
end_time = time.time()
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()

# Update total memory and time
performance.total_mem += peak
performance.total_time += (end_time - start_time)

return result

return wrapper
Loading