考慮到這類執行時間與如果或條件
1類
class myclass {
public static function myfunction($condition, $string)
{
if ($condition) {
// A lot of code here
// This is just a stupid example
echo $string;
}
}
}
2類
class myclass {
public static function myfunction($condition, $string)
{
// A lot of code here
// This is just a stupid example
echo $string;
}
}
和以下文件:
的文件1
myclass::myfunction(($i > 1), '$i is > of 1');
myclass::myfunction(($i > 2), '$i is > of 2');
myclass::myfunction(($i > 3), '$i is > of 3');
myclass::myfunction(($i > 4), '$i is > of 4');
myclass::myfunction(($i > 5), '$i is > of 5');
...
myclass::myfunction(($i > 50), '$i is > of 50'); // this is the amount of functions calls in my project more or less...
文件2
if ($i > 1) { myclass::myfunction('$i is > of 1'); }
if ($i > 2) { myclass::myfunction('$i is > of 2'); }
if ($i > 3) { myclass::myfunction('$i is > of 3'); }
if ($i > 4) { myclass::myfunction('$i is > of 4'); }
if ($i > 5) { myclass::myfunction('$i is > of 5'); }
...
if ($i > 50) { myclass::myfunction('$i is > of 50'); }
哪個文件將運行得更快(同時考慮2個不同的類別)在同一個工作基礎? PHP緩存類的方法請求或只是繼續尋找類,該方法,然後執行它?如果我將條件保留在方法內部(這樣方法將被執行),它會改變那麼多嗎?
當你進行基準測試時發生了什麼? :) – rdlowrey
在第一個示例中,每次都會運行'myfunction()',從而將該函數調用推送到執行堆棧。在第二個例子中,有時不會發生。我投第二個例子的速度更快,儘管可能可以忽略不計。最終,_just試試吧。 – Wiseguy
@rdlowrey,你認真嗎?我應該手動編輯所有50+條件,並在我的框架中添加if語句來進行基準測試嗎?我在理論上講是正確的,因爲很難完成基準測試...... – Shoe