Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua - If and and, what is faster?

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

?

like image 768
Fox Avatar asked Aug 29 '13 11:08

Fox


2 Answers

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.

like image 116
lhf Avatar answered Oct 10 '22 03:10

lhf


Lua is lazily evalulated, so it should make no difference.

The same effect would be true of short circuit logical and (&&) in c/c++

like image 36
OllieB Avatar answered Oct 10 '22 04:10

OllieB