Skip to content
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

Create decorators_ceyhun_binal.py #217

Closed
wants to merge 1 commit into from
Closed
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
45 changes: 45 additions & 0 deletions Week03/decorators_ceyhun_binal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import time

counter = {}
total_times = {}

def performance(func):
def _performance(*args, **kwargs):
function_name = func.__name__

if function_name not in counter:
counter[function_name] = 0
total_times[function_name] = 0

counter[function_name] += 1
start_time = time.time()
result = func(*args, **kwargs)
end_time = time.time()
execution_time = end_time - start_time
total_times[function_name] += execution_time

return result

return _performance

def print_function_summary():
for function_name in counter:
print(f"Function '{function_name}' took {total_times[function_name]:.6f} seconds.")
print(f"Total time for '{function_name}': {total_times[function_name]:.6f} seconds")
print(f"Function calls for '{function_name}': {counter[function_name]}")

@performance
def aggregate_function(arg1: int = 0, arg2: int = 0) -> int:
return arg1 + arg2

@performance
def multiple_function(arg1: float, arg2: float) -> float:
return arg1 * arg2

if __name__ == "__main__":
aggregate_function(3, 4)
multiple_function(2, 5)
multiple_function(534.4, 748.7)
multiple_function(489.25, 745.62)

print_function_summary()
Loading