Skip to content

Commit 8cadd51

Browse files
Create decorators_aysegul_yildiz.py
1 parent 5ae1705 commit 8cadd51

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import time
2+
import tracemalloc
3+
4+
def performans(func):
5+
"""Bir fonksiyonun zaman ve bellek tüketimini ölçen dekoratör."""
6+
7+
def sarmalayıcı(*args, **kwargs):
8+
# --- ÖNCE ---
9+
tracemalloc.start()
10+
baslangic = time.perf_counter()
11+
12+
# Fonksiyonun gerçek çalışması
13+
sonuc = func(*args, **kwargs)
14+
15+
# --- SONRA ---
16+
bitis = time.perf_counter()
17+
mevcut_bellek, maksimum_bellek = tracemalloc.get_traced_memory()
18+
tracemalloc.stop()
19+
20+
gecen_sure = bitis - baslangic
21+
22+
# Performans istatistiklerini güncelle
23+
performans.sayac += 1
24+
performans.toplam_sure += gecen_sure
25+
performans.toplam_bellek += maksimum_bellek
26+
27+
# Ölçüm sonuçlarını kullanıcıya göster
28+
print(f"Fonksiyon adı: {func.__name__}")
29+
print(f"Geçen süre: {gecen_sure:.10f} saniye")
30+
print(f"Bellek kullanımı: {maksimum_bellek} bayt")
31+
print(f"Toplam çağrı sayısı: {performans.sayac}\n")
32+
33+
return sonuc
34+
35+
return sarmalayıcı
36+
37+
# Dekoratörün durum değişkenlerini başlat
38+
performans.sayac = 0
39+
performans.toplam_sure = 0.0
40+
performans.toplam_bellek = 0

0 commit comments

Comments
 (0)