LUA: performance/efficiency 个链接函数

LUA: performance/efficiency of chaining functions

我一直认为 LUA(5.1 版)中的链接函数比先本地化然后调用下一个函数的性能更差。

是我错了还是我的简单基准不正确?

local clock = os.clock
local plugin = {}

function plugin:Test()
    return { SubTest = function() return "hello" end }
end

collectgarbage(); collectgarbage()
local start = clock()
for i=1, 1000000 do
    plugin:Test():SubTest()
end
print(clock() - start, collectgarbage("count") / 1024)

collectgarbage(); collectgarbage()
local start = clock()
for i=1, 1000000 do
    local module = plugin:Test()
    module:SubTest()
end
print(clock() - start, collectgarbage("count") / 1024)

chaining functions in LUA would perform worse than localizing it first and then calling the next function

仅当您多次使用本地(缓存)值时才是正确的。

is my simple benchmark incorrect?

基准测试中的两个循环都被编译为(几乎)相同的 Lua 字节码,因此它们的性能必须相等。