Skip to content
Open
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
46 changes: 46 additions & 0 deletions Week04/decorators_mustafafurkan_yilmaz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import time
import tracemalloc
import functools

def performance(func):
"""
A decorator that measures the execution time, memory usage,
and call count of the decorated functions.
"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
# Increment the global counter for the decorator
performance.counter += 1

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

try:
# Execute the actual function
result = func(*args, **kwargs)
finally:
# Calculate elapsed time
end_time = time.perf_counter()
elapsed_time = end_time - start_time

# Get memory usage (current, peak)
current_mem, peak_mem = tracemalloc.get_traced_memory()

# Stop tracking memory
tracemalloc.stop()

# Update global stats
performance.total_time += elapsed_time
performance.total_mem += peak_mem

return result

return wrapper

# Initialize the static attributes on the function object itself
# This allows all decorated functions to share these counters.
performance.counter = 0
performance.total_time = 0
performance.total_mem = 0