Skip to content

Create decorators_niyazi_cetinkaya.py #561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 21, 2024
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
49 changes: 49 additions & 0 deletions Week04/decorators_niyazi_cetinkaya.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import time
import tracemalloc

def performance(func):
"""
A decorator to keep track of how often a function is called,
how long it takes to run, and how much memory it uses.

Attributes:
counter: How many times the function has been called.
total_time: The total time the function has taken to run.
total_mem: The total memory used by the function.
"""

# Initialize the counters
performance.counter = 0
performance.total_time = 0
performance.total_mem = 0

def wrapper(*args, **kwargs):
# Start tracking memory
tracemalloc.start()

# Record the start time
start_time = time.time()

# Call the actual function
result = func(*args, **kwargs)

# Calculate how long it took
time_taken = time.time() - start_time

# Capture the peak memory usage
current_mem, peak_mem = tracemalloc.get_traced_memory()
tracemalloc.stop()

# Update the stats
performance.counter += 1
performance.total_time += time_taken
performance.total_mem += peak_mem # Peak memory during execution

# Friendly output
print(f"'{func.__name__}' was called {performance.counter} times.")
print(f"Total time so far: {performance.total_time:.4f} seconds.")
print(f"Total memory used: {performance.total_mem / 1024:.2f} KB.\n")

return result

return wrapper
Loading