"Rowne: If a variable isn't in the function parenthesis or doesn't have local behind it, it's global."
the control variables in for statements are also automatically localized. I still see addon authors declaring their control variables as local before using them. why?
a = "volkswagen"
for a = 1,4 do
print(a) -- will print numbers
end
print(a) -- will print "volkswagen"
I know this post is quite old and may have already been answered elsewhere but I will attempt to answer it anyway.
This is clearly from observation and there may be another reason of why it is done this way.
local message = "testing"
for i = 1, 4 do
if condition then
print(message)
end
end
for i = 1, 4 do
if condition then
local message = "testing"
print(message)
end
end
for i = 1, 4 do
local message = "testing"
if condition then
print(message)
end
end
In the first example message is defined first. This means it is only defined once. Within the second example it could be defined 8 times. Within each loop it could be defined each time the condition is true. This means it could also not be defined at all but on average it would be slower. It would also be defined again as nil when the if statement is ended. I may be wrong but I believe it is just as slow to change a value as to assign it again to the same value so in the third example it would still be defined 4 times.
Locals are designed to be more efficient than globals since they are deleted when no longer needed. But deleting them too early will result in them being redefined which would slow the program down.
Although I have a question about locals as well. Is it faster to define a local or to change its value?
local message
for i = 1, 4 do
message = "testing - line " .. i
print(message)
end
for i = 1, 4 do
local message = "testing - line " .. i
print(message)
end
Would the first or second example be faster? I've seen both in code and there seems to be no real advantage of one or the other except that the first may be easier to read for debugging.
Althaya
Rollback Post to RevisionRollBack
To post a comment, please login or register a new account.
Kaelten uploaded what Rowne sent him to http://rowne.wowace.com/
The classes should be in http://rowne.wowace.com/Classes.zip
I know this post is quite old and may have already been answered elsewhere but I will attempt to answer it anyway.
This is clearly from observation and there may be another reason of why it is done this way.
In the first example message is defined first. This means it is only defined once. Within the second example it could be defined 8 times. Within each loop it could be defined each time the condition is true. This means it could also not be defined at all but on average it would be slower. It would also be defined again as nil when the if statement is ended. I may be wrong but I believe it is just as slow to change a value as to assign it again to the same value so in the third example it would still be defined 4 times.
Locals are designed to be more efficient than globals since they are deleted when no longer needed. But deleting them too early will result in them being redefined which would slow the program down.
Although I have a question about locals as well. Is it faster to define a local or to change its value?
Would the first or second example be faster? I've seen both in code and there seems to be no real advantage of one or the other except that the first may be easier to read for debugging.
Althaya