Shop OBEX P1 Docs P2 Docs Learn Events
Local vs. Global Variables in SPIN — Parallax Forums

Local vs. Global Variables in SPIN

RumpleRumple Posts: 38
edited 2009-09-13 09:47 in Propeller 1
Is there any speed advantage (or disadvantage) to using·local variables instead of·global variables with a Spin method?·

Comments

  • KyeKye Posts: 2,200
    edited 2009-09-12 14:29
    None, just don't be stupid and try to use a local variable versuses a global variable.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Nyamekye,
  • RumpleRumple Posts: 38
    edited 2009-09-12 14:42
    Kye said...
    None, just don't be stupid and try to use a local variable versuses a global variable.

    That's kind of why I ask...using all globals·could distract my stupid genes·from things like putting coginit in loops·and shifting bits in the wrong direction.

    BTW, did I ever thank you for the StringEngine routines?· That was your work, wasn't it?

    Rump
  • localrogerlocalroger Posts: 3,452
    edited 2009-09-12 14:49
    Rumple, there is no speed difference between local vs. global vars, but there is a difference in that local vars don't take up any Hub RAM when not in use, since they live on the stack and they're released when the routine returns to its caller. (OTOH this means they need to be initialized, unlike global vars which are reliably set to 0 for you before the Spin image starts.)
  • BradCBradC Posts: 2,601
    edited 2009-09-13 04:31
    There is potentially a speed difference depending on how you declare your variables and what size you make them.

    We all know global variables get re-ordered by the compiler so you have
    LONGS
    WORDS
    BYTES

    And we all know that local variables are always long.
    Now when accessing the first 8 global or local longs, the compiler uses a different access method that is quite considerably faster.

    Remembering that local variables always start with the Result variable, the parameters, then your declared locals you can get a not-insignificant speed boost by using the first 8 locals or globals for frequently accessed variables. Bytes and words are always accessed slowly, as is any long outside of the first 8.

    I found this by accident one day when changing a variable from byte to long caused my loop to speed up considerably.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lt's not particularly silly, is it?
  • T ChapT Chap Posts: 4,223
    edited 2009-09-13 05:56
    Hey Brad, is there a simple loop you can post that shows this on a pin that we can see on a scope? Either using the longs above or below 8 or long vs byte to see the effect?
  • BradCBradC Posts: 2,601
    edited 2009-09-13 09:47
    CON
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 5_000_000
    
    
    Var
    '  long align[noparse][[/noparse]10]
      long fred
    PUB Start
      dira[noparse][[/noparse]0] := 1
      repeat
        fred := 1
        fred := fred + 1
        !outa[noparse][[/noparse]0]
    
    



    When fred is long my cro tells me P0 toggles at 21.7Khz
    when fred is byte it toggles at 18.5Khz
    When fred is long and "align" is uncommented it toggles at 18.8Khz

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lt's not particularly silly, is it?
Sign In or Register to comment.