Stack Level Local/Globals

Richard Gaskin ambassador at fourthworld.com
Mon Jul 4 17:15:49 EDT 2005


Dave Cragg wrote:
> 
> On 4 Jul 2005, at 20:07, Eric Chatonet wrote:
> 
>> Hi Dennis,
>>
>> I agree since I provide many tools for the community that are made  to 
>> run in the IDE.
> 
> 
> I agree too. :)
> 
>> So I never use globals but I prefer to refer to some custom  
>> properties stored in specific custom property sets in my plugins or  
>> in the revPreferences stack.
>> BTW, I found that referring to custom properties is as fast as  using 
>> global variables and they don't vanish when they are stored  in 
>> revPreferences.
>> That's great :-)
> 
> 
> Eric, my own testing shows that accessing custom properties is  
> significantly slower than accessing globals. (Over 20 times slower in  a 
> simple test.) When I have to refer to custom properties frequently  (for 
> example when going through hundreds of elements of a  
> customPropertySet), I usually copy them to a variable first. But  
> perhaps the access times vary by circumstances.
> 
> This was the most recent test I ran.
> 
> global gTest
> on mouseUp
>   set the cTest of me to 100
>   put 100 into gTest
>   put 100000 into tTimes
>   put the milliseconds into tStart
>   repeat tTimes
>     get gTest
>   end repeat
>   put the milliseconds - tStart into tOut
>   put the milliseconds into tStart
>   repeat tTimes
>     get the cTest of me
>   end repeat
>   put cr & the milliseconds - tStart after tOut
>   put tOut
> end mouseUp

That sounds slow expressed as relative figures, but when we look at the 
speed per access it's not so bad for many uses:

Milliseconds per access for-
    global:   0.00058
    property: 0.00441
    function: 0.00297

I usually use functions instead of constants for constant-like things, 
as they have the same read-only benefit of true constants but have the 
convenience of not requiring declaration.  In this test I had this 
function in the card script:

   function cTest
     return 100
   end cTest

I was surprised the function call was so fast relative to accessing a 
property, but either way isn't so bad for occassional access.  If you're 
doing a lot of processing of the data you could copy it to a local var 
during the heavy lifting.

-- 
  Richard Gaskin
  Fourth World Media Corporation
  ___________________________________________________________
  Ambassador at FourthWorld.com       http://www.FourthWorld.com



More information about the use-livecode mailing list