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
39 changes: 39 additions & 0 deletions Week04/decorators_heval_sogut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import time
import tracemalloc

def performance(func):

# This is the wrapper function.
def wrapper(*args, **kwargs):
"""Wrapper function that adds performance tracking."""

# --- "Before" logic ---
tracemalloc.start()
start_time = time.perf_counter()

# Execute the original function
result = func(*args, **kwargs)

# --- "After" logic ---
# Get performance metrics
end_time = time.perf_counter()
_current_mem, peak_mem = tracemalloc.get_traced_memory()
tracemalloc.stop()

elapsed_time = end_time - start_time

# Update the statistics
performance.counter += 1
performance.total_time += elapsed_time
performance.total_mem += peak_mem

return result

# The decorator returns the new wrapper function
return wrapper

# Initialize the state as attributes of the function object.
# This state will be shared across all uses of the @performance decorator.
performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0