Telegram Group & Telegram Channel
Forwarded from ChatGPT 4.5 | DeepSeek | Midjourney
دکوریتور lru_cache یکی از ابزارهای جالب و کاربردی در پایتون هست که به ما کمک می‌کنه تا عملکرد برنامه‌هامون رو با ذخیره‌سازی نتایج محاسبات قبلی بهبود ببخشیم. این دکوریتور به صورت خودکار نتایج تابع رو برای ورودی‌های تکراری ذخیره می‌کنه و وقتی هم که همون ورودی دوباره فراخوانی بشه، به جای محاسبه دوباره، نتیجه رو از حافظه می‌گیره.

فرض کن که یه تابع داریم که فاکتوریل یک عدد رو محاسبه می‌کنه. اگر بخوایم این تابع رو با lru_cache دکوریتور کنیم، به این صورت می‌شه:

from functools import lru_cache

@lru_cache(maxsize=None) # maxsize می‌تونه محدودیت حافظه رو تعیین کنه
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

print(factorial(5)) # خروجی: 120
print(factorial(5)) # اینجا دیگه محاسبه نمی‌شه و سریعاً از کش برمی‌گرده


اینجا وقتی که factorial(5) رو برای بار اول صدا می‌زنیم، تمام مراحل محاسبه انجام می‌شه و نتیجه 120 برمی‌گرده. اما وقتی دوباره همین تابع رو با همون ورودی صدا بزنیم، دیگه نیازی به محاسبه نیست و نتیجه از کش برمی‌گرده که خیلی سریع‌تره!

پس با استفاده از lru_cache می‌تونیم سرعت برنامه‌هامون رو بالا ببریم و کارایی رو بهبود بدیم.



tg-me.com/djangolearn_ir/1059
Create:
Last Update:

دکوریتور lru_cache یکی از ابزارهای جالب و کاربردی در پایتون هست که به ما کمک می‌کنه تا عملکرد برنامه‌هامون رو با ذخیره‌سازی نتایج محاسبات قبلی بهبود ببخشیم. این دکوریتور به صورت خودکار نتایج تابع رو برای ورودی‌های تکراری ذخیره می‌کنه و وقتی هم که همون ورودی دوباره فراخوانی بشه، به جای محاسبه دوباره، نتیجه رو از حافظه می‌گیره.

فرض کن که یه تابع داریم که فاکتوریل یک عدد رو محاسبه می‌کنه. اگر بخوایم این تابع رو با lru_cache دکوریتور کنیم، به این صورت می‌شه:

from functools import lru_cache

@lru_cache(maxsize=None) # maxsize می‌تونه محدودیت حافظه رو تعیین کنه
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)

print(factorial(5)) # خروجی: 120
print(factorial(5)) # اینجا دیگه محاسبه نمی‌شه و سریعاً از کش برمی‌گرده


اینجا وقتی که factorial(5) رو برای بار اول صدا می‌زنیم، تمام مراحل محاسبه انجام می‌شه و نتیجه 120 برمی‌گرده. اما وقتی دوباره همین تابع رو با همون ورودی صدا بزنیم، دیگه نیازی به محاسبه نیست و نتیجه از کش برمی‌گرده که خیلی سریع‌تره!

پس با استفاده از lru_cache می‌تونیم سرعت برنامه‌هامون رو بالا ببریم و کارایی رو بهبود بدیم.

BY جنگولرن


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/djangolearn_ir/1059

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Telegram has exploded as a hub for cybercriminals looking to buy, sell and share stolen data and hacking tools, new research shows, as the messaging app emerges as an alternative to the dark web.An investigation by cyber intelligence group Cyberint, together with the Financial Times, found a ballooning network of hackers sharing data leaks on the popular messaging platform, sometimes in channels with tens of thousands of subscribers, lured by its ease of use and light-touch moderation.

In many cases, the content resembled that of the marketplaces found on the dark web, a group of hidden websites that are popular among hackers and accessed using specific anonymising software.“We have recently been witnessing a 100 per cent-plus rise in Telegram usage by cybercriminals,” said Tal Samra, cyber threat analyst at Cyberint.The rise in nefarious activity comes as users flocked to the encrypted chat app earlier this year after changes to the privacy policy of Facebook-owned rival WhatsApp prompted many to seek out alternatives.telegram from ms


Telegram جنگولرن
FROM USA