From 30ceb3d7c775e23e45d1655cc5bb070bc02dc3fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 16:21:10 +0300 Subject: [PATCH 1/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Week04/decorators_miray_cengil.py diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py new file mode 100644 index 00000000..5241f2dc --- /dev/null +++ b/Week04/decorators_miray_cengil.py @@ -0,0 +1,62 @@ +import time +import tracemalloc +from functools import wraps + +class PerformanceTracker: + """ + A decorator class that tracks the performance of a function. + Attributes: + counter (int): Number of times the decorated function is called. + total_time (float): Total time the function has taken. + total_mem (int): Total memory the function has consumed in bytes. + """ + def __init__(self, func): + self.func = func + self.counter = 0 + self.total_time = 0 + self.total_mem = 0 + + def __call__(self, *args, **kwargs): + """Measures the execution time and memory usage of the function.""" + self.counter += 1 + + # Start time measurement + start_time = time.perf_counter() + + # Start memory tracking + tracemalloc.start() + + result = self.func(*args, **kwargs) + + # Stop memory tracking + current, peak = tracemalloc.get_traced_memory() + tracemalloc.stop() + + # Stop time measurement + end_time = time.perf_counter() + + # Update total time and memory + self.total_time += (end_time - start_time) + self.total_mem += peak + + # Output statistics (optional for debugging) + print(f"Function '{self.func.__name__}' called {self.counter} times") + print(f"Total time taken: {self.total_time:.4f} seconds") + print(f"Total memory used: {self.total_mem / 1024:.4f} KB") + + return result + +def performance_tracker(func): + """A simple function decorator to track performance.""" + return PerformanceTracker(func) + +# Example function to test +@performance_tracker +def compute_squares(n): + """Returns a list of squared numbers up to n.""" + return [i**2 for i in range(n)] + +# Running the decorated function multiple times +compute_squares(1000) +compute_squares(2000) +compute_squares(3000) From a70c034d7cae7ea92bbee68f5ff62a0e95cae0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 16:30:25 +0300 Subject: [PATCH 2/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index 5241f2dc..2f6f60ff 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -46,7 +46,7 @@ def __call__(self, *args, **kwargs): return result -def performance_tracker(func): +def performance(func): """A simple function decorator to track performance.""" return PerformanceTracker(func) From 00dde6fe18507caee1cbb99c47075e5e58e55010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 16:33:25 +0300 Subject: [PATCH 3/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index 2f6f60ff..3c9d691f 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -51,7 +51,7 @@ def performance(func): return PerformanceTracker(func) # Example function to test -@performance_tracker +@performance def compute_squares(n): """Returns a list of squared numbers up to n.""" return [i**2 for i in range(n)] From 979d03c7a546ac224a2e7f8707455a85464f7123 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 16:49:48 +0300 Subject: [PATCH 4/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index 3c9d691f..c58af950 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -1,6 +1,5 @@ import time import tracemalloc -from functools import wraps class PerformanceTracker: """ @@ -57,6 +56,7 @@ def compute_squares(n): return [i**2 for i in range(n)] # Running the decorated function multiple times +if __name__ == "__main__": compute_squares(1000) compute_squares(2000) compute_squares(3000) From 773d2a6330ea2f4dd6d26273db3619f630fb9cfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 16:52:58 +0300 Subject: [PATCH 5/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index c58af950..e7c16cf9 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -1,5 +1,6 @@ import time import tracemalloc +from functools import wraps class PerformanceTracker: """ @@ -57,6 +58,6 @@ def compute_squares(n): # Running the decorated function multiple times if __name__ == "__main__": -compute_squares(1000) -compute_squares(2000) -compute_squares(3000) + compute_squares(1000) + compute_squares(2000) + compute_squares(3000) From 8fa47559fbde0f2e8768e23aeb1648b35b69eda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 17:22:39 +0300 Subject: [PATCH 6/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index e7c16cf9..becf8f9c 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -45,6 +45,14 @@ def __call__(self, *args, **kwargs): print(f"Total memory used: {self.total_mem / 1024:.4f} KB") return result + + def get_stats(self): + """Returns the performance statistics.""" + return { + "counter": self.counter, + "total_time": self.total_time, + "total_mem": self.total_mem + } def performance(func): """A simple function decorator to track performance.""" @@ -61,3 +69,8 @@ def compute_squares(n): compute_squares(1000) compute_squares(2000) compute_squares(3000) + + # Access performance statistics + tracker = compute_squares + stats = tracker.get_stats() + From 619c18193f33376854118ffec23e1ff2cfb2de8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Wed, 23 Oct 2024 19:44:32 +0300 Subject: [PATCH 7/8] decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 109 ++++++++++++++---------------- 1 file changed, 50 insertions(+), 59 deletions(-) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index becf8f9c..86676a4c 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -1,76 +1,67 @@ +import os +import inspect import time +import random import tracemalloc -from functools import wraps -class PerformanceTracker: +def performance(func): """ - A decorator class that tracks the performance of a function. + A decorator to measure the performance of a function. + Attributes: - counter (int): Number of times the decorated function is called. - total_time (float): Total time the function has taken. - total_mem (int): Total memory the function has consumed in bytes. + 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. """ - def __init__(self, func): - self.func = func - self.counter = 0 - self.total_time = 0 - self.total_mem = 0 - - def __call__(self, *args, **kwargs): - """Measures the execution time and memory usage of the function.""" - self.counter += 1 - # Start time measurement - start_time = time.perf_counter() + # Initialize the counters + performance.counter = 0 + performance.total_time = 0.0 + performance.total_mem = 0.0 - # Start memory tracking + def wrapper(*args, **kwargs): + # Start tracking memory tracemalloc.start() - - result = self.func(*args, **kwargs) - - # Stop memory tracking - current, peak = tracemalloc.get_traced_memory() + + # 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() - - # Stop time measurement - end_time = time.perf_counter() - - # Update total time and memory - self.total_time += (end_time - start_time) - self.total_mem += peak - - # Output statistics (optional for debugging) - print(f"Function '{self.func.__name__}' called {self.counter} times") - print(f"Total time taken: {self.total_time:.4f} seconds") - print(f"Total memory used: {self.total_mem / 1024:.4f} KB") - return result + # Update the stats + performance.counter += 1 + performance.total_time += time_taken + performance.total_mem += peak_mem # Peak memory during execution - def get_stats(self): - """Returns the performance statistics.""" - return { - "counter": self.counter, - "total_time": self.total_time, - "total_mem": self.total_mem - } + return result + + return wrapper -def performance(func): - """A simple function decorator to track performance.""" - return PerformanceTracker(func) +# Test functions +@performance +def example_function(x): + time.sleep(x) + return x ** 2 -# Example function to test @performance -def compute_squares(n): - """Returns a list of squared numbers up to n.""" - return [i**2 for i in range(n)] +def memory_intensive_function(size): + return [random.randint(0, 100) for _ in range(size)] -# Running the decorated function multiple times -if __name__ == "__main__": - compute_squares(1000) - compute_squares(2000) - compute_squares(3000) +# Execute test functions +example_function(1) +example_function(2) +example_function(3) +memory_intensive_function(1000000) - # Access performance statistics - tracker = compute_squares - stats = tracker.get_stats() - +# Access 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.") From 58d28a2f30528df327c0427e973a5a4acc034430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miray=20CENG=C4=B0L?= Date: Mon, 4 Nov 2024 02:40:38 +0300 Subject: [PATCH 8/8] Update decorators_miray_cengil.py --- Week04/decorators_miray_cengil.py | 57 ++++++++++++++----------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/Week04/decorators_miray_cengil.py b/Week04/decorators_miray_cengil.py index 86676a4c..b7b83466 100644 --- a/Week04/decorators_miray_cengil.py +++ b/Week04/decorators_miray_cengil.py @@ -1,45 +1,38 @@ -import os -import inspect import time -import random -import tracemalloc +import sys def performance(func): """ A decorator to measure the performance of a function. Attributes: - counter: How many times the function has been called. + 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 memory used by the function. + total_mem: The total memory used by the function in bytes. """ - # Initialize the counters - performance.counter = 0 - performance.total_time = 0.0 - performance.total_mem = 0.0 + 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): - # Start tracking memory - tracemalloc.start() - # Record the start time start_time = time.time() - # Call the actual function + # Call the actual function and calculate memory usage result = func(*args, **kwargs) + memory_usage = sys.getsizeof(result) - # Calculate how long it took + # Calculate time taken time_taken = time.time() - start_time - # Capture the peak memory usage - current_mem, peak_mem = tracemalloc.get_traced_memory() - tracemalloc.stop() - - # Update the stats + # Update the statistics performance.counter += 1 performance.total_time += time_taken - performance.total_mem += peak_mem # Peak memory during execution + performance.total_mem += memory_usage return result @@ -53,15 +46,15 @@ def example_function(x): @performance def memory_intensive_function(size): - return [random.randint(0, 100) for _ in range(size)] + return [0] * size -# Execute test functions -example_function(1) -example_function(2) -example_function(3) -memory_intensive_function(1000000) - -# Access 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.") +if __name__ == "__main__": + # Run test functions + example_function(1) + example_function(2) + memory_intensive_function(1000000) + + # Display 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.")