Skip to content

Commit 6b5e122

Browse files
authored
Refactor functions and improve performance tracking
Refactor custom_power function and enhance fn_w_counter with performance measurement.
1 parent 71f5b39 commit 6b5e122

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

Week04/functions_Helin_Harman.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
custom_power = lambda x, /, e=1: x**e
2+
3+
result = custom_power(3)
4+
print(result)
5+
6+
def custom_equation(x=0, y=0, /, a=1, *, b=1, c=1) -> float:
7+
"""
8+
This function calculates
9+
(x*a + y*b)/c
10+
"""
11+
return (x*a + y*b)/c
12+
13+
result1 = custom_equation(3, 4, 5, b=4, c=2)
14+
print(result1)
15+
16+
17+
import inspect
18+
19+
global_counter = 0
20+
caller_log = {}
21+
22+
def fn_w_counter (c: int, d: dict) -> tuple[int, dict]:
23+
global global_counter
24+
global caller_log
25+
26+
caller_name = 'unknown_run'
27+
try:
28+
# inspect.stack()[1] normalde '__main__' veya 'fonksiyon_adı'nı bulur.
29+
caller_name = inspect.stack()[1].f_globals.get('__name__', 'system_call')
30+
except (IndexError, AttributeError, ValueError):
31+
caller_name = 'ide_console' # Hata durumunda bunu logla
32+
33+
global_counter += 1
34+
caller_log[caller_name] = caller_log.get(caller_name, 0) + 1
35+
36+
return (global_counter, caller_log.copy())
37+
38+
def helper_test(val):
39+
return fn_w_counter(val, {})
40+
41+
if __name__ == '__main__':
42+
print("--- fn_w_counter Gelişmiş Testi ---")
43+
44+
45+
fn_w_counter(1, {})
46+
47+
count2, log2 = fn_w_counter(2, {})
48+
print(f"1. Aşamadan Toplam: {count2}, Log: {log2}")
49+
50+
51+
count3, log3 = helper_test(3)
52+
print(f"2. Aşamadan Toplam: {count3}, Log: {log3}")
53+
54+
55+
import time
56+
import tracemalloc
57+
from functools import wraps
58+
59+
def performance(func):
60+
@wraps(func) #docstringi korur
61+
def _performance(*args, **kwargs):
62+
63+
64+
tracemalloc.start()
65+
t1 = time.perf_counter()
66+
67+
result = func(*args, **kwargs)
68+
69+
current, peak = tracemalloc.get_traced_memory() #bellek kullanımı
70+
tracemalloc.stop()
71+
t2 = time.perf_counter()
72+
73+
elapsed_time = t2 - t1
74+
75+
76+
_performance.counter += 1
77+
_performance.total_time += elapsed_time
78+
_performance.total_mem += peak
79+
80+
return result
81+
82+
_performance.counter = 0
83+
_performance.total_time = 0.0
84+
_performance.total_mem = 0
85+
86+
return _performance
87+
88+
@performance
89+
def calculate_process_time(n):
90+
data = list(range(n))
91+
result = sum(data)
92+
return result
93+
94+
print("Performance dekoratif testi")
95+
96+
resulta =calculate_process_time(500000)
97+
print(resulta)
98+
99+
calculate_process_time(1000000)
100+
101+
print(" ")
102+
103+
print("sonuçlar : ")
104+
105+
print(f"Toplam Çağrı Sayısı (counter): {calculate_process_time.counter}")
106+
print(f"Toplam Süre (total_time): {calculate_process_time.total_time:.4f} saniye")
107+
print(f"Toplam Bellek (total_mem): {calculate_process_time.total_mem} byte")

0 commit comments

Comments
 (0)