View Single Post
08-29-13, 03:49 AM   #36
Kagura
A Fallenroot Satyr
Join Date: Nov 2008
Posts: 21
On the subject to declaring variables to their most confining scope next should be noted:
Lua Code:
  1. local a
  2. for i=1, 5 do
  3.     a = i
  4. end
Code:
-- Compiles into

main <test.lua:0,0> (7 instructions, 28 bytes at 0x80049128)
0+ params, 5 slots, 0 upvalues, 5 locals, 2 constants, 0 functions
        1       [3]     LOADK           1 -1    ; 1
        2       [3]     LOADK           2 -2    ; 5
        3       [3]     LOADK           3 -1    ; 1
        4       [3]     FORPREP         1 1     ; to 6
        5       [4]     MOVE            0 4
        6       [3]     FORLOOP         1 -2    ; to 5
        7       [5]     RETURN          0 1
]]

Lua Code:
  1. for i=1, 5 do
  2.     local a
  3.     a = i
  4. end

Code:
-- Compiles into

main <test.lua:0,0> (8 instructions, 32 bytes at 0x80049128)
0+ params, 5 slots, 0 upvalues, 5 locals, 2 constants, 0 functions
        1       [2]     LOADK           0 -1    ; 1
        2       [2]     LOADK           1 -2    ; 5
        3       [2]     LOADK           2 -1    ; 1
        4       [2]     FORPREP         0 2     ; to 7
        5       [3]     LOADNIL         4 4
        6       [4]     MOVE            4 3
        7       [2]     FORLOOP         0 -3    ; to 5
        8       [5]     RETURN          0 1
]]
and

Lua Code:
  1. for i=1, 5 do
  2.     local a = i
  3. end

Code:
-- Compiles into

main <test.lua:0,0> (7 instructions, 28 bytes at 0x80049128)
0+ params, 5 slots, 0 upvalues, 5 locals, 2 constants, 0 functions
        1       [2]     LOADK           0 -1    ; 1
        2       [2]     LOADK           1 -2    ; 5
        3       [2]     LOADK           2 -1    ; 1
        4       [2]     FORPREP         0 1     ; to 6
        5       [3]     MOVE            4 3
        6       [2]     FORLOOP         0 -2    ; to 5
        7       [4]     RETURN          0 1
As you can guess, the second is a bit slower, but it should also be noted that the gain from this is only going to be noticable for large amount of variable declarations.

While doing a large amount of variable declarations, next should also be noted (I'll leave the compiled versions out since I don't want to pollute the thread):

Lua Code:
  1. function UnitAura(unitId, index)
  2. end
  3.  
  4. local startTime, endTime
  5. startTime = os.clock()
  6. for j = 1, 1000000 do
  7.     local name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID, canApplyAura, isBossDebuff, value1, value2, value3
  8.     for i = 1, 40 do
  9.         name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID, canApplyAura, isBossDebuff, value1, value2, value3 = UnitAura("player", i)
  10.     end
  11. end
  12. endTime = os.clock()
  13.  
  14. print(endTime-startTime)
  15.  
  16. startTime = os.clock()
  17. for j = 1, 1000000 do
  18.     for i = 1, 40 do
  19.         local name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID, canApplyAura, isBossDebuff, value1, value2, value3 = UnitAura("player", i)
  20.     end
  21. end
  22. endTime = os.clock()
  23.  
  24. print(endTime-startTime)
  25.  
  26. startTime = os.clock()
  27. for j = 1, 1000000 do
  28.     for i = 1, 40 do
  29.         local name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID, canApplyAura, isBossDebuff, value1, value2, value3
  30.         name, rank, icon, count, dispelType, duration, expires, caster, isStealable, shouldConsolidate, spellID, canApplyAura, isBossDebuff, value1, value2, value3 = UnitAura("player", i)
  31.     end
  32. end
  33. endTime = os.clock()
  34.  
  35. print(endTime-startTime)
  36.  
  37. --- Results
  38. $ lua test.lua
  39. 5.859
  40. 2.562
  41. 6.375

If you really want to optimize your addon, you need to look at compiled code and understand how function calls/lua stack works.
  Reply With Quote