Local vs. Global Variables in SPIN
Rumple
Posts: 38
Is there any speed advantage (or disadvantage) to using·local variables instead of·global variables with a Spin method?·
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Nyamekye,
BTW, did I ever thank you for the StringEngine routines?· That was your work, wasn't it?
Rump
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?
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?