[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Local Variables
- From: Michael Welsh Duggan <mwd@...>
- Date: Fri, 08 Aug 2014 15:59:37 -0400
Michael Welsh Duggan <mwd@cert.org> writes:
> Michael Welsh Duggan <mwd@cert.org> writes:
>
>> Mason Mackaman <masondeanm@aol.com> writes:
>>
>>> So to make a program run as fast as possible should you localize
>>> everything in _ENV you’re going to use in your program?
>>
>> I was going to say, "No; only if you're going to use it more than once."
>> But my empirical studies seem to demonstrate otherwise. The included
>> program has three functions, all which use a global sub-function "foo".
>> The first function uses it directly as a global reference. The second
>> stashes it in a local variable and uses it from there. The third has it
>> pre-stashed as an upvalue. In each run, the function is referenced one
>> more time than in the previous run. The results look like they speak
>> for themselves. I tried to be careful in my methodology, but I wouldn't
>> be surprised if I didn't account for something in my experiment. Feel
>> free to attempt to replicate my results (numbers are number of seconds):
>
> I take that back. There was a trivial bug in my code. The following
> results seem closer to what I would expect. Code attached.
>
> 1 global = 3 local = 3 upvalue = 3
> 2 global = 5 local = 4 upvalue = 4
> 3 global = 6 local = 6 upvalue = 5
> 4 global = 8 local = 6 upvalue = 6
> 5 global = 10 local = 8 upvalue = 7
> 6 global = 11 local = 8 upvalue = 9
> 7 global = 12 local = 10 upvalue = 9
> 8 global = 14 local = 11 upvalue = 10
> 9 global = 16 local = 12 upvalue = 11
> 10 global = 17 local = 14 upvalue = 12
And a third run with the repeat set three times higher demonstrates that
if you only use a global once, it's more efficient than stashing it in a
local. (Though the upvalue should always be faster.) This makes sense,
since you have to access the global to stash it in the local. If you
only use the local once, there's no win.
1 global = 9 local = 10 upvalue = 8
2 global = 14 local = 13 upvalue = 12
3 global = 19 local = 16 upvalue = 16
4 global = 23 local = 21 upvalue = 18
5 global = 28 local = 24 upvalue = 21
6 global = 33 local = 26 upvalue = 25
--
Michael Welsh Duggan
(mwd@cert.org)
- References:
- Local Variables, Mason Mackaman
- Re: Local Variables, Coda Highland
- Re: Local Variables, Elias Barrionovo
- Re: Local Variables, Mason Mackaman
- Re: Local Variables, Steven Degutis
- Re: Local Variables, Mason Mackaman
- Re: Local Variables, Elias Barrionovo
- Re: Local Variables, Mason Mackaman
- Re: Local Variables, Michael Welsh Duggan
- Re: Local Variables, Michael Welsh Duggan