Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua optimize memory

I what to optimize my code. I have 3 option don't know which is better for memory in Lua:

1)

local Test = {}
    Test.var1 = function ()
        -- Code
    end

    Test.var2 = function ()
        -- Code
    end

2) Or

function var1()
    -- Code
end

function var2()
    -- Code
end

3) Or maybe

local var1 = function ()
    -- Code
end

local var2 = function ()
    -- Code
end
like image 589
itdxer Avatar asked Oct 04 '13 12:10

itdxer


2 Answers

Quoting from Lua Programming Gem, the two maxims of program optimization:

  • Rule #1: Don’t do it.
  • Rule #2: Don’t do it yet. (for experts only)

Back to your examples, the second piece of code is a little bit worse as the access to global ones is slower. But the performance difference is hardly noticeable.

It depends on your needs, the first one uses an extra table than the third one, but the namespace is cleaner.

like image 199
Yu Hao Avatar answered Oct 06 '22 17:10

Yu Hao


None will really affect memory, barring the use of a table in #1 (so some 40 bytes + some per entry).

If its performance you want, then option #3 is far better, assuming you can access said functions at the local scope.

like image 40
Ian Avatar answered Oct 06 '22 18:10

Ian