简体   繁体   English

Lua优化内存

[英]Lua optimize memory

I what to optimize my code. 我要优化我的代码。 I have 3 option don't know which is better for memory in Lua: 我有3个选项,不知道哪个更适合Lua中的内存:

1) 1)

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

    Test.var2 = function ()
        -- Code
    end

2) Or 2)或

function var1()
    -- Code
end

function var2()
    -- Code
end

3) Or maybe 3)或者也许吧

local var1 = function ()
    -- Code
end

local var2 = function ()
    -- Code
end

Quoting from Lua Programming Gem , the two maxims of program optimization: 引用Lua Programming Gem ,程序优化的两个格言:

  • Rule #1: Don't do it. 规则#1:不要这样做。
  • Rule #2: Don't do it yet. 规则#2:不要这样做。 (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. 这取决于您的需求,第一个使用额外的表而不是第三个,但命名空间更清晰。

None will really affect memory, barring the use of a table in #1 (so some 40 bytes + some per entry). 没有人会真正影响内存,除非在#1中使用表(所以每个条目大约40个字节+一些)。

If its performance you want, then option #3 is far better, assuming you can access said functions at the local scope. 如果你想要它的性能,那么假设你可以在本地范围内访问所述函数,则选项#3要好得多。

If it's about memory usage more than processing and you're using object-oriented programming where you're instantiating multiple instances of Test as you showed above, you have a fourth option with metatables. 如果它是关于内存使用而不是处理,并且你正在使用面向对象的编程,你正在实例化上面显示的多个Test实例,那么你有第四个选项与metatables。

TestMt = {}
TestMt.func1 = function(self, ...)
    ...
end
TestMt.func2 = function(self, ...)
    ...
end
TestMt.func3 = function(self, ...)
    ...
end

function new_test()
    local t = {}
    t.data = ...
    setmetatable(t, {__index = TestMt})
    return t
end

foo = new_test()
foo:func1()
foo:func2()
foo:func3()

If you're doing object-oriented kind of programming, metatables can lead to a massive savings in memory (I accidentally used over 1 gigabyte once for numerous mathematical vectors this way, only to reduce it down to 40 megabytes by using the metatable). 如果您正在进行面向对象的编程,metatables可以大大节省内存(我不小心使用了超过1 GB的数据载体,只需通过使用metatable将其减少到40 MB)。

If it's not about objects and tables that get instantiated many times, and just about organizing your globally-accessible functions, worrying about memory here is ridiculous. 如果它不是关于多次实例化的对象和表,而是关于组织全局可访问的函数,那么担心内存就太荒谬了。 It's like putting the entirety of your lua code into one file in order to reduce file system overhead. 这就像将整个lua代码放入一个文件中以减少文件系统开销。 You're talking about such negligible savings that you should really need an extraordinary use case backed by meticulous measurements to even concern yourself with that. 你说的是这种微不足道的节省,你真的需要一个非常精确的测量支持的特殊用例,甚至可以关注你自己。

If it's about processing, then you can get some small improvements by keeping your global functions out of nested tables, and by favoring locals when possible. 如果它是关于处理的,那么你可以通过将全局函数保留在嵌套表之外,并在可能的情况下支持本地函数来获得一些小的改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM