我檢查這個PHP website benchmark進行檢查switch statement
VS if else if statement
。我看到這樣的結果:PHP switch語句VS if ifif語句基準
是否有開關和結構之間的區別?調用1'000x
- 141%
*if and elseif (using ==)*
總時間:165μsview代碼 - 139%
*if, elseif and else (using ==)*
總時間:162μsview代碼 - 110%
*if and elseif (using ===)*
總時間:128μsview代碼 - 100%
*if, elseif and else (using ===)*
總時間:117微秒查看代碼 - 149%
*switch/caseTotal*
時間:174μsview代碼 - 181%
*switch/case/default*
總時間:211μsview代碼
在結果我看到if else if
更快(+ **100 %** *if, elseif and else (using ===)* Total time: 117 µsview code
)。
這個基準是真的,if, elseif and else (using ===)
更好更快,因爲switch語句?!
因爲'=='會嘗試進行類型轉換(如果這兩個值不相等),所以'==='比==快。 switch-case-statement只有2個案例(和一個默認案例)。如果你有更多的案例,我認爲它會變得更快(相對於if-elseif)。但是,你的問題到底是什麼? – AbcAeffchen
基準瑕疵:是第一種情況/是否比上次更常見或不常見? –