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: |

The SSE was the first modern stock exchange to open in China, with trading commencing in 1990. It has now grown to become the largest stock exchange in Asia and the third-largest in the world by market capitalization, which stood at RMB 50.6 trillion (US$7.8 trillion) as of September 2021. Stocks (both A-shares and B-shares), bonds, funds, and derivatives are traded on the exchange. The SEE has two trading boards, the Main Board and the Science and Technology Innovation Board, the latter more commonly known as the STAR Market. The Main Board mainly hosts large, well-established Chinese companies and lists both A-shares and B-shares.

Telegram auto-delete message, expiring invites, and more

elegram is updating its messaging app with options for auto-deleting messages, expiring invite links, and new unlimited groups, the company shared in a blog post. Much like Signal, Telegram received a burst of new users in the confusion over WhatsApp’s privacy policy and now the company is adopting features that were already part of its competitors’ apps, features which offer more security and privacy. Auto-deleting messages were already possible in Telegram’s encrypted Secret Chats, but this new update for iOS and Android adds the option to make messages disappear in any kind of chat. Auto-delete can be enabled inside of chats, and set to delete either 24 hours or seven days after messages are sent. Auto-delete won’t remove every message though; if a message was sent before the feature was turned on, it’ll stick around. Telegram’s competitors have had similar features: WhatsApp introduced a feature in 2020 and Signal has had disappearing messages since at least 2016.

telegram from vn


Telegram جنگولرن
FROM USA