From 7a3ff20dd7eb3d0653e410b51aaab6244a535587 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=96zt=C3=BCrk?= <97892689+burakozturk1@users.noreply.github.com> Date: Mon, 6 Jan 2025 21:48:42 +0300 Subject: [PATCH 1/2] Create decorators_burak_ozturk.py --- Week04/decorators_burak_ozturk.py | 36 +++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 Week04/decorators_burak_ozturk.py diff --git a/Week04/decorators_burak_ozturk.py b/Week04/decorators_burak_ozturk.py new file mode 100644 index 00000000..bbb186ba --- /dev/null +++ b/Week04/decorators_burak_ozturk.py @@ -0,0 +1,36 @@ +import time +import tracemalloc + +def performance(func): + setattr(performance, '_count', 0) + setattr(performance, '_time', 0.0) + setattr(performance, '_memory', 0.0) + + def wrapper(*args, **kwargs): + count = getattr(performance, '_count') + 1 + setattr(performance, '_count', count) + + start = time.time() + tracemalloc.start() + + try: + func(*args, **kwargs) + except Exception as exc: + print(f"Error: {exc}") + finally: + end = time.time() + curr, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + elapsed = end - start + total_time = getattr(performance, '_time') + elapsed + total_mem = getattr(performance, '_memory') + peak + + setattr(performance, '_time', total_time) + setattr(performance, '_memory', total_mem) + + print(f"{func.__name__}: Calls={count}, Time={elapsed:.4f}s, " + f"Memory={peak / 1024:.1f}KB, TotalTime={total_time:.4f}s, " + f"TotalMem={total_mem / 1024:.1f}KB") + + return wrapper From 0464144b599b614451c2de1d8ed548a3af95bb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20=C3=96zt=C3=BCrk?= <97892689+burakozturk1@users.noreply.github.com> Date: Sat, 11 Jan 2025 20:25:37 +0300 Subject: [PATCH 2/2] Update decorators_burak_ozturk.py --- Week04/decorators_burak_ozturk.py | 72 +++++++++++++++++++++++-------- 1 file changed, 55 insertions(+), 17 deletions(-) diff --git a/Week04/decorators_burak_ozturk.py b/Week04/decorators_burak_ozturk.py index bbb186ba..c77b3ff8 100644 --- a/Week04/decorators_burak_ozturk.py +++ b/Week04/decorators_burak_ozturk.py @@ -1,36 +1,74 @@ import time import tracemalloc + def performance(func): - setattr(performance, '_count', 0) - setattr(performance, '_time', 0.0) - setattr(performance, '_memory', 0.0) + """ + A decorator to measure the performance of a function. + Attributes: + counter: The number of times the function has been called. + total_time: The total time the function has taken to run. + total_mem: The total peak memory used by the function in bytes. + """ + + # Initialize attributes if they don't already exist + if not hasattr(performance, 'counter'): + performance.counter = 0 + if not hasattr(performance, 'total_time'): + performance.total_time = 0.0 + if not hasattr(performance, 'total_mem'): + performance.total_mem = 0 def wrapper(*args, **kwargs): - count = getattr(performance, '_count') + 1 - setattr(performance, '_count', count) + # Increment call counter + performance.counter += 1 - start = time.time() + # Measure execution time and memory usage + start_time = time.time() tracemalloc.start() try: - func(*args, **kwargs) + result = func(*args, **kwargs) except Exception as exc: print(f"Error: {exc}") + result = None finally: - end = time.time() - curr, peak = tracemalloc.get_traced_memory() + end_time = time.time() + current, peak = tracemalloc.get_traced_memory() tracemalloc.stop() - elapsed = end - start - total_time = getattr(performance, '_time') + elapsed - total_mem = getattr(performance, '_memory') + peak + # Update performance metrics + time_taken = end_time - start_time + performance.total_time += time_taken + performance.total_mem += peak - setattr(performance, '_time', total_time) - setattr(performance, '_memory', total_mem) + # Print stats for the current function call + print(f"{func.__name__}: Calls={performance.counter}, Time={time_taken:.4f}s, " + f"Memory={peak / 1024:.1f}KB, TotalTime={performance.total_time:.4f}s, " + f"TotalMem={performance.total_mem / 1024:.1f}KB") - print(f"{func.__name__}: Calls={count}, Time={elapsed:.4f}s, " - f"Memory={peak / 1024:.1f}KB, TotalTime={total_time:.4f}s, " - f"TotalMem={total_mem / 1024:.1f}KB") + return result return wrapper + + +@performance +def example_function(x): + return x * x + + +@performance +def memory_intensive_function(size): + return [0] * size + + +if __name__ == "__main__": + # Run test functions + example_function(1) + example_function(2) + memory_intensive_function(1000000) + + # Display final performance metrics + print(f"Function called {performance.counter} times.") + print(f"Total time taken: {performance.total_time:.4f} seconds.") + print(f"Total memory used: {performance.total_mem / 1024:.2f} KB.")