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
?
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.
Lua is lazily evalulated, so it should make no difference.
The same effect would be true of short circuit logical and (&&) in c/c++
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With