Lua - If and and, what is faster?

纵然是瞬间 提交于 2019-12-10 14:34:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!