Avoid use global variables
Global variables is slow.
Also if you put
local gg = gg
At top of your script it can speed up it. Just one line.
Now see tests:
local n = 1000000
local t = os.clock()
for i = 1, n do
gg.isVisible()
end
t = os.clock() - t
print('use global gg: '..t..' seconds')
local gg = gg
local t = os.clock()
for i = 1, n do
gg.isVisible()
end
t = os.clock() - t
print('use local gg: '..t..' seconds')
a, b, c = 1, 2, 3
local t = os.clock()
for i = 1, n do
c = a + b
end
t = os.clock() - t
print('use global vars: '..t..' seconds')
local a, b, c = 1, 2, 3
local t = os.clock()
for i = 1, n do
c = a + b
end
t = os.clock() - t
print('use local vars: '..t..' seconds')
Results:
use global gg: 2.138 seconds
use local gg: 1.6 seconds
use global vars: 2.068 seconds
use local vars: 0.727 seconds
It is not big difference, because I run it on powerful emulator. On real device it can be more slow.
You can see disassembled code - for global vars need more Lua instructions, so it more slow in any case.
Upvalue too slow, Because of that better define local copy of var in places where you need optimization. For example huge math.