當我用lua檢查兩個條件時,哪種方式在運行時間更快?Lua - 如果和和,什麼更快?
if bool and somefuntion() then
do stuff
end
或
if bool then
if somefuntion() then
do stuff
end
end
?
當我用lua檢查兩個條件時,哪種方式在運行時間更快?Lua - 如果和和,什麼更快?
if bool and somefuntion() then
do stuff
end
或
if bool then
if somefuntion() then
do stuff
end
end
?
兩個片段運行luac -l -p
,你會發現它們產生完全相同的字節碼。所以,寫一些更清楚的東西給你。
的原因它們是相同的是,Lua使用短路評價和和或,如通過OllieB提及。
可能有一個理論上的偏見,但實際上我懷疑它是可衡量的。你有任何sytax的問題嗎? – Jan
不,但函數執行時間很長。我只是想知道如果它運行時布爾是假的.. – Fox
啊,那麼也許你應該問這個問題。但下面的答案是正確的。 – Jan