问题
When I check two conditions with and in lua, which way is faster in running time?
if bool and somefuntion() then
do stuff
end
or
if bool then
if somefuntion() then
do stuff
end
end
?
回答1:
Run luac -l -p on both fragments and you'll see that they generate exactly the same bytecode. So write whatever is clearer for you.
The reason they are the same is that Lua uses short-circuit evaluation for and and or, as mentioned by OllieB.
回答2:
Lua is lazily evalulated, so it should make no difference.
The same effect would be true of short circuit logical and (&&) in c/c++
来源:https://stackoverflow.com/questions/18509877/lua-if-and-and-what-is-faster