Go Back   WowAce Forums > General > Lua Code Discussion
Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding].

Reply
 
Thread Tools
Old 2 Weeks Ago   #1
Beoko
Newbie
 
Join Date: Oct 2008
Posts: 3
Default Local Variable Optimization

I've been curious about this for a while now. Assuming scope is not an issue, would it be faster to do:

Code:
local Variable1, Variable2

local function Func()
   Variable1 = something...
   Variable2 = something else...
end
Or:

Code:
local function Func()
   local Variable1 = something...
   local Variable2 = something else...
end
Or, would the speed be equal? Also, would the first method be more memory efficient than the second method?
Beoko is offline   Reply With Quote
Old 2 Weeks Ago   #2
Arrowmaster
Hero Member
 
Join Date: Apr 2006
Posts: 764
Default Re: Local Variable Optimization

It depends entirely on where you are going to use the variables.
Arrowmaster is offline   Reply With Quote
Old 2 Weeks Ago   #3
Graveeater
Junior Member
 
Join Date: Aug 2005
Posts: 63
Default Re: Local Variable Optimization

The following example would probably be more helpful:

Code:
for k,v in pairs(largeTable) do
    local var = someFunc(k,v)
    -- do something with var
end
compared to
Code:
local var
for k,v in pairs(largeTable) do
    var = someFunc(k,v)
    -- do something with var
end
I suppose the second example will be faster, but I am not sure. In a compiled language, these probably would not matter at all.

Last edited by Graveeater; 2 Weeks Ago at 06:55 PM. Reason: forgotten code tags
Graveeater is offline   Reply With Quote
Old 2 Weeks Ago   #4
Arrowmaster
Hero Member
 
Join Date: Apr 2006
Posts: 764
Default Re: Local Variable Optimization

No you are wrong.
Arrowmaster is offline   Reply With Quote
Old 2 Weeks Ago   #5
Flickor
Amazing Member
 
Join Date: Sep 2006
Posts: 1,028
Default Re: Local Variable Optimization

There would be two reasons why you should use the local variable outside the function:
1. If that function is called VERY often (like from a onupdate script or a heavy event like CLEU or UNIT_AURA)
2. If you are going to use the same variable in multiple functions or somewhere else in your code.
__________________
p3lim.net
Flickor is offline   Reply With Quote
Old 2 Weeks Ago   #6
Beoko
Newbie
 
Join Date: Oct 2008
Posts: 3
Default Re: Local Variable Optimization

Quote:
Originally Posted by Arrowmaster View Post
No you are wrong.
Why is he wrong, though?

Quote:
Originally Posted by Flickor View Post
There would be two reasons why you should use the local variable outside the function:
1. If that function is called VERY often (like from a onupdate script or a heavy event like CLEU or UNIT_AURA)
2. If you are going to use the same variable in multiple functions or somewhere else in your code.
I'm aware of the purpose for declaring it outside or inside of the function. I'm not aware of the actual amount of time/memory I would save by doing one or the other. I'm assuming the time will be negligible, but would the memory be? Am I assuming incorrectly? Pie or cake?
Beoko is offline   Reply With Quote
Old 2 Weeks Ago   #7
Tekkub
Wiki Master
 
Tekkub's Avatar
 
Join Date: Feb 2005
Posts: 4,853
Default Re: Local Variable Optimization

Don't concern yourself with memory for locals, it doesn't involve gc. Just declare the variable within the scope you need it.
Tekkub is offline   Reply With Quote
Old 2 Weeks Ago   #8
Farmbuyer
Senior Member
 
Join Date: Feb 2005
Posts: 494
Default Re: Local Variable Optimization

Quote:
Originally Posted by Beoko View Post
Why is he wrong, though?
For some reason Arrow gets a free pass on these forums to be a complete dick without ever getting called on it.

Quote:
I'm assuming the time will be negligible, but would the memory be? Am I assuming incorrectly? Pie or cake?
The two examples in Graveater's post are not quite the same, primarily because the 'var' reference in the second example is not cleared. In the first example, once var goes out of scope at the end of the function call, whatever memory it was referencing will be available for garbage collection. Not so at the end of the second example. [edit: talking about whatever's returned from "someFunc", not the local variable itself]

You could adjust the second example to be
Code:
local var
for k,v in pairs(largeTable) do
    var = someFunc(k,v)
    -- do something with var
    var = nil
end
and then it would be functionally equivalent to the first... but then you haven't gained anything over the first, and lost some clarity in the process.
Farmbuyer is offline   Reply With Quote
Old 2 Weeks Ago   #9
Beoko
Newbie
 
Join Date: Oct 2008
Posts: 3
Default Re: Local Variable Optimization

Thanks for the replies. That's about all I needed to know.
Beoko is offline   Reply With Quote
Old 1 Week Ago   #10
Xinhuan
Asian Sheep Lover
 
Xinhuan's Avatar
 
Join Date: Aug 2007
Location: Singapore
Posts: 3,418
Default Re: Local Variable Optimization

Quote:
Originally Posted by Graveeater View Post
The following example would probably be more helpful:

Code:
for k,v in pairs(largeTable) do
    local var = someFunc(k,v)
    -- do something with var
end
compared to
Code:
local var
for k,v in pairs(largeTable) do
    var = someFunc(k,v)
    -- do something with var
end
I suppose the second example will be faster, but I am not sure. In a compiled language, these probably would not matter at all.
All locals in a function are allocated memory in the stack. Scope is merely a matter of compiler issue, and doesn't apply in compiled code.

In your first example, the function containing your loop will allocate 8 bytes of memory for "var". That 8 bytes of memory exists until your function returns and the stack unwinds, but according to language rules, "var" is only accessible inside your for-loop's block of code.

In your second example, the same 8 bytes of memory also exists until your function returns and the stack unwinds, but according to language rules, "var" is accessible from the point it is declared until the end of the containing block of code.

In essence, there is no performance difference. [Note that I've not talked about upvalues (referring to local variables outside of the function you are in) such as file-scope variables].
__________________
Author/Maintainer of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, Routes, HandyNotes and some others.
Xinhuan is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT. The time now is 12:45 AM.