Intune
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 50 |
|
|
| 07 Feb 2012 09:57 PM |
So, I've been hearing that local variables can be accessed faster than global variables.
i.e. var1 = 6 local var2 = 6
print(var2) -- Would be faster than print(var1) -- This
So I ran a test myself on several different scales. Here is what I did:
local non_local = 0 local _local = 0
local local_var = 6 non_local_var = 6
for i = 1,100000 do local now = tick() local var = non_local_var * 2 local later = tick() non_local = non_local + (later - now) end
for i = 1,100000 do local now = tick() local var = local_var * 2 local later = tick() _local = _local + (later - now) end
print(_local) print(non_local)
As you can see, with each loop, the only difference in variable type non_local_var and local_var. The variables non_local and _local are both defined as local, so that has no difference. Now here is an example output:
0.12509655952454 0.13324928283691
But then I thought, "wait, what if cache has something to do with this?" Well, I flipped the loops around and...
0.13003206253052 0.12779951095581
My goal was to see what the time difference was to access a variable and perform a calculation on it, for both locals and globals. I'm pretty sure my tests are accurate, but if they are not, PLEASE tell me, because I'd rather write local because it makes the script look cooler. :) |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 07 Feb 2012 10:09 PM |
local = Variable that gets nil'd when the scope exits. Obviously less memory would be used; meaning its faster.
-Or am I thinking about memory leaks? |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 07 Feb 2012 10:10 PM |
| But realize on roblox it makes such a small difference to your coding. |
|
|
| Report Abuse |
|
|
nate890
|
  |
| Joined: 22 Nov 2008 |
| Total Posts: 21686 |
|
|
| 07 Feb 2012 10:16 PM |
"local variables can be accessed faster than global variables." "accessed faster"
What do you mean?
<'+1 Post. Ujelly?'> |
|
|
| Report Abuse |
|
|
Intune
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 50 |
|
|
| 07 Feb 2012 10:18 PM |
| I do realize that, but the local variable doesn't get nil'd, because it was defined outside the loop. The local and global variables have the same scopes, since they were defined at the start of the code. |
|
|
| Report Abuse |
|
|
Intune
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 50 |
|
|
| 07 Feb 2012 10:19 PM |
@nate890
To access a variable 'x' means you access the data stored in memory under key 'x'. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
|
| 07 Feb 2012 10:22 PM |
Bah, whatever you want call it. The script throws the variable away after the scope ends. Are you happy? |
|
|
| Report Abuse |
|
|
Intune
|
  |
| Joined: 02 Feb 2012 |
| Total Posts: 50 |
|
|
| 07 Feb 2012 10:25 PM |
| Yes, but it does it for both loops, meaning it should the speed is realtively consistent. But in my tests, 100% of my what, 14 runs proved that global variables can actually be accessed faster. |
|
|
| Report Abuse |
|
|
KingBoo
|
  |
| Joined: 16 Jul 2007 |
| Total Posts: 8495 |
|
| |
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 07 Feb 2012 11:31 PM |
Your test is meaningless.
You call "tick()" twice inside of every single loop body, which takes at least 100x longer than accessing a variable, either locally or globally. Whatever results you do get... the noise of how long tick() happens to take each run is totally drowning out any effect the variable access might have. (As for the reason you do see a consistent difference, there may be odd effects to do with memory access patterns which make the specific single variable access of global VS local work badly in that one spot with the other code, but I can assure you on average local variables are indeed faster. It's difficult to explain how that sort of thing can happen to someone without a knowledge of C and lower level programming)
Any any rate it is 100% certain that local variable accesses will always take longer than global variable accesses on average, simply because of the way each is implemented.
If you really want to see the difference do something like this:
local a, b, c = 1, 2, 3 d, e, f = 1, 2, 3
local standard = tick() for i = 1, 10000000 do end standard = tick()-standard
local start = tick() for i = 1, 10000000 do a, b, c = a*b*c, a*b*c, a*b*c end print("Local: "..((tick()-start)-standard))
start = tick() for i = 1, 10000000 do d, e, f = d*e*f, d*e*f, d*e*f end print("Global: "..((tick()-start)-standard))
--> Local: 0.70 --> Global: 3.73
That's more than a 5x difference!
PS: "I do realize that, but the local variable doesn't get nil'd, because it was defined outside the loop. The local and global variables have the same scopes, since they were defined at the start of the code." That would not make a difference anyways. Allocating local variables in the loop as opposed to outside of it is almost free, due to the way it's implemented internally. When you declare a local variable nothing actually happens other than the VM knowing ahead of time when the code is compiled that it should move it's working space one "ahead" to make a spot where the local can go. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 04:48 AM |
Defining variables as local instead of global, regardless of the scope, WILL be beneficial to your script's speed.
Your test isn't representative, because tick() by itself already fakes the results, as the time it may take to complete will vary, but varies on a much bigger range than the impact of using locals instead of globals.
The best way to understand it would be to look at Lua's source code (can be browsed online at lua dot org slash source), but you'd need to know C to understand most of it, anyways. |
|
|
| Report Abuse |
|
|
|
| 08 Feb 2012 11:32 AM |
Lua should make all variables local by default and replace 'local' with a 'global' keywork so for a variable to be outside of scope when defined in scope, it must be specifically said so.
it'd make more sense. most languages do it. |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
|
| 08 Feb 2012 11:49 AM |
@Oysi
No, in that case you'd do what most languages do:
global a = 5
script.Parent.Touched:connect(function(b) global a //When we say a, we want the global a to be affected in this scope. I know PHP does this. a = b end)
That'd behave like normal now, so would this:
global a = 5
script.Parent.Touched:connect(function(b) global a = b end) |
|
|
| Report Abuse |
|
|
Oysi
|
  |
| Joined: 06 Jul 2009 |
| Total Posts: 9058 |
|
| |
|
|
| 08 Feb 2012 01:45 PM |
it's a standard- makes code much more efficient without repeated local
Although I think the word local like 50 times at the start of a line for var initialisation looks kinda cool |
|
|
| Report Abuse |
|
|
ENET
|
  |
| Joined: 01 Jan 2010 |
| Total Posts: 4820 |
|
|
| 08 Feb 2012 04:52 PM |
at the top of the scope i define all locals...
local a, b, c; a = 1; b = 2; do local d, e; c = 3; e = 5; d = 4; --tada... end |
|
|
| Report Abuse |
|
|