View Single Post
08-16-05, 09:07 AM   #6
Littlejohn
A Warpwood Thunder Caller
AddOn Author - Click to view addons
Join Date: Jun 2005
Posts: 90
That's a complicated question... I'm going to answer it in three separate posts. A warning though: each post is gonna get tougher to understand!

Lua and WoW process code a whole file at a time. A file is first read into memory, then converted to internal "bytecode" form, and finally executed. Variable assignment happens in the execution phase, so it's generally more efficient to delay initialization until you are sure you will need the variable. Compare these two examples:
Code:
function f1()
    local x
    if 1 == 0 then
	x = { 'a', 'b', 'c' }
	print(x.getn())
    end
end

function f2()
    local x = { 'a', 'b', 'c' }
    if 1 == 0 then
	print(x.getn())
    end
end
f2() runs 6 times slower than f1() because f2() always creates, assigns and destroys a table even though it doesn't use it.

The flip side of this is that you want your code to be clean and simple so you (and others) can understand it easily. People often expect global variables to be initialized at the top of the file, so even if you can make things a little faster by delaying initialization until necessary, it's probably not worth it because it will confuse people.

You want to go with option 1 and initialize the variables all at once:
Code:
Global_options = {'a','b','c'};
Warrior_options = {'d','e','f'};
Rogue_options = {'g','h','i'};
  Reply With Quote