R
Rob Janecek
Hello,
In some coding advice I saw on the internet, we are urged to "avoid
referring to global or static variables inside the tightest loops".
I also read (elsewhere) that access time to global/static and
automatic variables can be different on different hardware
(i.e., automatic variables cannot always be counted on being
faster).
What is then the justification for the above quote? Could it be
that the compiler might avoid using registers for global/static
variables? But then why could it not just copy the variable to a
register and restore it's value after the loop's over (assuming,
of course, that there are no function calls or other side
effects in that tight loop)? I can see a problem with multi-
threading but is this not a special case that could be treated
separately by the compiler only when it arises?
[I have no problem following the advice, I'm just curious why it
is true...]
In some coding advice I saw on the internet, we are urged to "avoid
referring to global or static variables inside the tightest loops".
I also read (elsewhere) that access time to global/static and
automatic variables can be different on different hardware
(i.e., automatic variables cannot always be counted on being
faster).
What is then the justification for the above quote? Could it be
that the compiler might avoid using registers for global/static
variables? But then why could it not just copy the variable to a
register and restore it's value after the loop's over (assuming,
of course, that there are no function calls or other side
effects in that tight loop)? I can see a problem with multi-
threading but is this not a special case that could be treated
separately by the compiler only when it arises?
[I have no problem following the advice, I'm just curious why it
is true...]