|
||||||
| Lua Code Discussion You scared? Terrified. Mortified. Petrified. Stupefied... by [coding]. | ||||||
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Newbie
Join Date: Oct 2008
Posts: 3
|
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 Code:
local function Func() local Variable1 = something... local Variable2 = something else... end |
|
|
|
|
|
#2 |
|
Hero Member
Join Date: Apr 2006
Posts: 764
|
It depends entirely on where you are going to use the variables.
|
|
|
|
|
|
#3 |
|
Junior Member
Join Date: Aug 2005
Posts: 63
|
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
Code:
local var
for k,v in pairs(largeTable) do
var = someFunc(k,v)
-- do something with var
end
Last edited by Graveeater; 2 Weeks Ago at 06:55 PM. Reason: forgotten code tags |
|
|
|
|
|
#4 |
|
Hero Member
Join Date: Apr 2006
Posts: 764
|
No you are wrong.
|
|
|
|
|
|
#5 |
|
Amazing Member
Join Date: Sep 2006
Posts: 1,028
|
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 |
|
|
|
|
|
#6 | |
|
Newbie
Join Date: Oct 2008
Posts: 3
|
Why is he wrong, though?
Quote:
|
|
|
|
|
|
|
#7 |
|
Wiki Master
Join Date: Feb 2005
Posts: 4,853
|
Don't concern yourself with memory for locals, it doesn't involve gc. Just declare the variable within the scope you need it.
|
|
|
|
|
|
#8 | |
|
Senior Member
Join Date: Feb 2005
Posts: 494
|
For some reason Arrow gets a free pass on these forums to be a complete dick without ever getting called on it.
Quote:
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
|
|
|
|
|
|
|
#9 |
|
Newbie
Join Date: Oct 2008
Posts: 3
|
Thanks for the replies.
That's about all I needed to know. |
|
|
|
|
|
#10 | |
|
Asian Sheep Lover
Join Date: Aug 2007
Location: Singapore
Posts: 3,418
|
Quote:
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. |
|
|
|
|
![]() |
«
Previous Thread
|
Next Thread
»
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|
All times are GMT. The time now is 12:45 AM.
WowAce Forums





That's about all I needed to know.
