問題描述
如果您了解其背后的過程,這可能是顯而易見的.但是,例如,當您在產品頁面上使用 Mage::registry('current_product')
時,您是否只是在引用某些內容已經加載"了還是每次運行該行代碼時都加載它?
This is probably something obvious if you know the process behind it.. But when you use Mage::registry('current_product')
on a product page, for example, are you merely referencing something that is already "loaded" or are you loading it every time you run that line of code?
換句話說,哪個更有效率?(下面是偽代碼)
In other words, which is more efficient? (pseudocode below)
Mage::registry('current_product')->getName() over and over
或者...
$temp = Mage::registry('current_product') then
$temp->getName() over and over
推薦答案
調用
Mage::registry('current_product')->getName()
一遍又一遍地稍微比
$temp = Mage::registry('current_product') then
$temp->getName() over and over
但這并沒有那么糟糕,我會非常擔心.如果您要設置編碼風格,請選擇第二種.如果你有一堆舊代碼和前者,不要擔心它的性能.
But it's not so bad that I'd be super concerned about. If you're setting a coding style, pick the second. If you have a bunch of old code with the former, don't worry about its performance.
當您調用 Mage::registry('current_product')
時,產品本身不會從數據庫中重新加載 — 此方法所做的只是返回一個存儲在靜態數組中的對象引用Mage
類的.
The product itself won't be reloaded from the database when you call Mage::registry('current_product')
—?all this method does is return an object reference that's been stored on a static array of the Mage
class.
我說前者效率稍低的原因是,如果你看一下registry
The reason I say the former will be slightly less efficient is, if you take a look at the source of registry
#File: app/Mage.php
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
您將看到 Magento 在返回值之前檢查鍵是否已設置.從理論上講,這項檢查比從 registry
中抓取一次然后重用變量更多的工作.
You'll see Magento check if the key is set before returning a value. This check, theoretically, is more work that grabbing it from registry
once and then reusing the variable.
但是,實際上,在這成為真正的問題之前,您將遇到更大的瓶頸.
However, practically speaking, you're going to have bigger bottlenecks before this is a real problem.
這篇關于Magento:Mage::registry('current_product') 有效嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!