Lua内部函数vs模块级函数

时间:2011-05-22 09:09:53

标签: lua

对于另一个函数内部的函数,Lua是否在每次调用外部函数时“实例化”内部函数?如果是这样,以下代码中bar()的效果会比foo()还差吗?

local function a()
  print 'a'
end

function foo()
  a()
end

function bar()
  function b()
    print 'b'
  end

  b()
end

3 个答案:

答案 0 :(得分:16)

测试用例1:ab都是全局的,没有嵌入。

$ cat junk.lua ; time lua junk.lua
function a(n)
    return n + 1
end

function b(n)
    return a(n)
end

for c = 1, 10000000 do
    b(c)
end


real    0m1.743s
user    0m1.740s
sys 0m0.000s

用户时间: 1.74s

测试用例2:a本地,b全局,无嵌入。

local function a(n)
    return n + 1
end

function b(n)
    return a(n)
end

for c = 1, 10000000 do
    b(c)
end


real    0m1.388s
user    0m1.390s
sys 0m0.000s

用户时间 1.39s

测试案例3:ab都是本地的,没有嵌入。

$ cat junk.lua ; time lua junk.lua
local function a(n)
    return n + 1
end

local function b(n)
    return a(n)
end

for c = 1, 10000000 do
    b(c)
end


real    0m1.194s
user    0m1.200s
sys 0m0.000s

用户时间 1.2s

测试案例4:a嵌入ba全局,b本地。

$ cat junk.lua ; time lua junk.lua
local function b(n)
    function a(n)
        return n + 1
    end

    return a(n)
end

for c = 1, 10000000 do
    b(c)
end


real    0m2.804s
user    0m2.790s
sys 0m0.000s

用户时间: 2.79s 。 (!)

测试用例5:a嵌入b,本地。

$ cat junk.lua ; time lua junk.lua
local function b(n)
    local function a(n)
        return n + 1
    end

    return a(n)
end

for c = 1, 10000000 do
    b(c)
end


real    0m2.540s
user    0m2.530s
sys 0m0.000s

用户时间: 2.53s

结果摘要:

  1. 编写测试以确认或否认对性能的直觉很容易。你应该这样做,而不是依赖众包的答案。 (你看,人群常常是错的。)
  2. 使函数局部而不是全局函数对函数调用开销有显着的积极影响。 (当这两个函数都是本地函数时,在这组测试用例中大约要好30%。)
  3. 在另一个函数中嵌入函数会对函数调用开销产生严重的负面影响。 (当这两个函数都是本地函数时,这组测试用例大约差110%。)
  4. 我是否提到测试可能是一个很好的想法,只能信任人群?

答案 1 :(得分:5)

bar会慢一些,因为每次都会创建一个新的函数对象。如果你想在函数内声明函数,你可能想要返回一个闭包。

local bar = function()
  local f = function()
    -- Lots of stuff...
  end
  local g = function()
    -- Also lots of stuff
  end
  return function()
    -- Do something with f and g...
  end
end

local created_f = bar()
created_f()
created_f()  -- Now you can skip the function initialization.

答案 2 :(得分:0)

Roberto已经在这样的Lua Performance Tips上写了pretty comprehensive article

相关问题