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 and Signal Havens for Right-Wing Extremists

Since the violent storming of Capitol Hill and subsequent ban of former U.S. President Donald Trump from Facebook and Twitter, the removal of Parler from Amazon’s servers, and the de-platforming of incendiary right-wing content, messaging services Telegram and Signal have seen a deluge of new users. In January alone, Telegram reported 90 million new accounts. Its founder, Pavel Durov, described this as “the largest digital migration in human history.” Signal reportedly doubled its user base to 40 million people and became the most downloaded app in 70 countries. The two services rely on encryption to protect the privacy of user communication, which has made them popular with protesters seeking to conceal their identities against repressive governments in places like Belarus, Hong Kong, and Iran. But the same encryption technology has also made them a favored communication tool for criminals and terrorist groups, including al Qaeda and the Islamic State.

Can I mute a Telegram group?

In recent times, Telegram has gained a lot of popularity because of the controversy over WhatsApp’s new privacy policy. In January 2021, Telegram was the most downloaded app worldwide and crossed 500 million monthly active users. And with so many active users on the app, people might get messages in bulk from a group or a channel that can be a little irritating. So to get rid of the same, you can mute groups, chats, and channels on Telegram just like WhatsApp. You can mute notifications for one hour, eight hours, or two days, or you can disable notifications forever.

telegram from de


Telegram جنگولرن
FROM USA