This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
TheOddByte's profile picture

Locals vs Globals Benchmark test

Started by TheOddByte, 06 June 2015 - 02:01 PM
TheOddByte #1
Posted 06 June 2015 - 04:01 PM
Because I haven't posted anything in a long time I'll post a little program that I found and modified to show that it's better to use locals, rather than global variables. It's what you expect it to be, a benchmark test, first it tests with local variables, then with global variables.

Screenshot( not really needed, but I posted one anyway )


For those who're lazy( like me ) you can download with the command below

pastebin get 90kzBp0P benchmark

Direct link: http://pastebin.com/90kzBp0P
SquidDev #2
Posted 06 June 2015 - 04:25 PM
I'd of expected it to be a slightly higher difference - it is one array index operation rather than 3 or 4 hashmap lookups (first in the environment, then for __index in the metatable, then in the parent environment). It might be interesting to check other micro-optimisations (a * (1/3) vs a / 3, a * a vs a ^ 2, etc…).
Creator #3
Posted 06 June 2015 - 04:58 PM
I'd of expected it to be a slightly higher difference - it is one array index operation rather than 3 or 4 hashmap lookups (first in the environment, then for __index in the metatable, then in the parent environment). It might be interesting to check other micro-optimisations (a * (1/3) vs a / 3, a * a vs a ^ 2, etc…).
Would it even be worth it?
SquidDev #4
Posted 06 June 2015 - 05:01 PM
I'd of expected it to be a slightly higher difference - it is one array index operation rather than 3 or 4 hashmap lookups (first in the environment, then for __index in the metatable, then in the parent environment). It might be interesting to check other micro-optimisations (a * (1/3) vs a / 3, a * a vs a ^ 2, etc…).
Would it even be worth it?
That's what I've always wondered… I'm not sure if LuaJ implements constant folding so a * (1/3) might not even have an effect. a * a is quicker than a ^ 2 but I don't know by how much.
Edited on 06 June 2015 - 03:02 PM
TheOddByte #5
Posted 06 June 2015 - 05:41 PM
- snip -
Thanks for the feedback, but this was mostly only made to demonstrate why it's better to use locals, as it can speedup slow programs.
Most new users on these forums tend to not use locals, which is kind of annoying sometimes, but it was the same with me when I was new here :P/>
Geforce Fan #6
Posted 06 June 2015 - 05:46 PM
- snip -
Thanks for the feedback, but this was mostly only made to demonstrate why it's better to use locals, as it can speedup slow programs.
Most new users on these forums tend to not use locals, which is kind of annoying sometimes, but it was the same with me when I was new here :P/>
It's really not the most important thing they could learn–not when they're trying to learn functions, how strings and numbers work, string manipulation, etc. It sorta comes toward the end of the basics, and that's because the programs will at least function without locals.
A bigger problem with the newbies is that they go straight ahead and attempt an OS–something that requires a lot of Lua knowledge to do. The 'os'es are typically pointless; the most they feature is a graphic UI, usually without multitasking, and usually more difficult to use and less featured than CraftOS itself.


haha I finished quite a bit faster than you. My processor(I don't think graphics or RAM really matter to CC) is an i5 quad-core @ 3.5GHz. What's yours?
Edited on 06 June 2015 - 03:58 PM
Creator #7
Posted 06 June 2015 - 05:56 PM
How come local are faster than globals?
TheOddByte #8
Posted 06 June 2015 - 06:27 PM
haha I finished quite a bit faster than you. My processor(I don't think graphics or RAM really matter to CC) is an i5 quad-core @ 3.5GHz. What's yours?
I've got a Intel(R) Core(TM) i5-4300M @ 2.60GHz, but I'm on my school laptop so I'm not surprised :P/>

How come local are faster than globals?
TOBIT explained it pretty well here: http://www.computercraft.info/forums2/index.php?/topic/15650-repeating-a-function-every-minute/page__view__findpost__p__150185
Creator #9
Posted 06 June 2015 - 06:40 PM
haha I finished quite a bit faster than you. My processor(I don't think graphics or RAM really matter to CC) is an i5 quad-core @ 3.5GHz. What's yours?
I've got a Intel® Core™ i5-4300M @ 2.60GHz, but I'm on my school laptop so I'm not surprised :P/>

How come local are faster than globals?
TOBIT explained it pretty well here: http://www.computerc...post__p__150185
So, you have a school laptop and a not school laptop? And the school laptop is that good?
TheOddByte #10
Posted 06 June 2015 - 06:45 PM
So, you have a school laptop and a not school laptop? And the school laptop is that good?
Well yeah, it's surprisingly good for a school laptop, it's one of the perks of going in an IT school :P/>
Geforce Fan #11
Posted 07 June 2015 - 03:07 AM
So, you have a school laptop and a not school laptop? And the school laptop is that good?
what?!?!
You've just said he has a school laptop and not a school laptop…
flaghacker #12
Posted 07 June 2015 - 07:43 AM
So, you have a school laptop and a not school laptop? And the school laptop is that good?
what?!?!
You've just said he has a school laptop and not a school laptop…

He probably means
a non-school laptop
TheOddByte #13
Posted 07 June 2015 - 11:39 AM
-snip-
What I think he meant with that is that it's better than ordinary school laptops( I think ).
Creator #14
Posted 07 June 2015 - 11:45 AM
-snip-
What I think he meant with that is that it's better than ordinary school laptops( I think ).
What I meant is that you have a very good school laptop. And alone the fact that you have a laptop for yourself from the school surprises me.
TheOddByte #15
Posted 07 June 2015 - 11:57 AM
What I meant is that you have a very good school laptop. And alone the fact that you have a laptop for yourself from the school surprises me.
Well that's going in a school in sweden gets you, especially when it's an IT school! :D/> :P/>
Creator #16
Posted 07 June 2015 - 12:23 PM
What I meant is that you have a very good school laptop. And alone the fact that you have a laptop for yourself from the school surprises me.
Well that's going in a school in sweden gets you, especially when it's an IT school! :D/> :P/>

I should really move to sweden