Lua optimization tipps
I found some interesting optimization tipps
http://lua-users.org/wiki/OptimisationTips
for example
Another example from here
http://stackoverflow.com/questions/1546 ... ua-program
Also this PDF has some interesting tipps
http://www.lua.org/gems/sample.pdf
For example
some of this things I found in mods and I think, there is potential for optimization in many mods
http://lua-users.org/wiki/OptimisationTips
for example
t[#t+1] = 0 is faster than table.insert(t, 0)
Multiplication x*0.5 is faster than division x/2
x*x is faster than x^2
Another example from here
http://stackoverflow.com/questions/1546 ... ua-program
function ipairs
When iterating a table, the function overhead from ipairs does not justify it's use. To iterate a table, instead useYour phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
for k=1, #tbl do local v = tbl[k];
It does exactly the same without the function call overhead (pairs actually returns another function which is then called for every element in the table while #tbl is only evaluated once). It's a lot faster, even if you need the value. And if you don't...
Also this PDF has some interesting tipps
http://www.lua.org/gems/sample.pdf
For example
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
function foo (x)
for i = 1, 1000000 do
x = x + math.sin(i)
end
return x
end
print(foo(10))
We can optimize it by declaring sin once, outside function foo:Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
- Code: Select all
local sin = math.sin
function foo (x)
for i = 1, 1000000 do
x = x + sin(i)
end
return x
end
print(foo(10))
This second code runs 30% faster than the original one
some of this things I found in mods and I think, there is potential for optimization in many mods