I'm really curious if this really make sense in CS2D Scripts. I know that there's something like code optimization and https://springrts.com/wiki/Lua_Performance. Does it really important in CS2D Scripts?
The most important thing I ask about is
for _, i in pairs() doto measure the real difference.
I have made online test code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
local tab = {} for i = 1, 1000 do tab[i] = { a = false } end local x x = os.clock() for i = 1, 10 do for _, i in pairs(tab) do i.a = _ end end print(string.format("elapsed time: %.4f\n", os.clock() - x)) x = os.clock() for i = 1, 10 do for i = 1, #tab do tab[i].a = i end end print(string.format("elapsed time: %.4f\n", os.clock() - x))
Looping like 1000 elements, 10x times per second. The results was:
elapsed time: 0.0013
elapsed time: 0.0010
Almost no difference.
I wonder if CS2D Servers will freeze to calculate similar stuff for large data...? like looping 500 custom mobs and do math calculations in ms100 event.
Other data results:
pairs: 3.078 (217%)
ipairs: 3.344 (236%)
for i=1,x do: 1.422 (100%)
for i=1,#atable do 1.422 (100%)
for i=1,atable_length do: 1.562 (110%)