Lua - If和and,什么更快? [英] Lua - If and and, what is faster?

查看:929
本文介绍了Lua - If和and,什么更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我和lua检查两个条件时,哪个方式的运行时间更快?

When I check two conditions with and in lua, which way is faster in running time?

if bool and somefuntion() then
    do stuff
end

if bool then
    if somefuntion() then
        do stuff
    end
end

推荐答案

在两个片段上运行 luac -l -p ,您将看到它们生成完全相同的字节码。所以写一些更清楚的东西。

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.

它们相同的原因是Lua对以及使用短路评估或者,如OllieB所述。

The reason they are the same is that Lua uses short-circuit evaluation for and and or, as mentioned by OllieB.

这篇关于Lua - If和and,什么更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆