global/static vars inside loops 
Author Message
 global/static vars inside loops

Greetings,

In Michael Lee's insightful and handy "Optimization of Computer
Programs in C" 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 its 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...]

Thanks,
Christos
--



Sat, 16 Mar 2002 03:00:00 GMT  
 global/static vars inside loops

Quote:

> In Michael Lee's insightful and handy "Optimization of Computer
> Programs in C" we are urged to "avoid referring to global or
> static variables inside the tightest loops".
> What is then the justification for the above quote?

I don't think there is any.
Any decent modern C compiler distributes (factors) high-overhead
operations out of loops, for example caching nonlocal references.
It is best to write the program as clearly as possible and *then*
worry about micro-optimization tweaks *if* you find that they are
needed to meet performance goals.
--



Sun, 17 Mar 2002 03:00:00 GMT  
 global/static vars inside loops



:> Greetings,
:>
:> In Michael Lee's insightful and handy "Optimization of Computer
:> Programs in C" we are urged to "avoid referring to global or
:> static variables inside the tightest loops".
:>
:> ....

: Optimizations about public data verses automatic storage will be compiler
: and architecture specific.  To learn something about optimization, look here
: {C optimization}:
: http://www.ontek.com/mikey/optimization.html
:
: ...

Well, this was the site from where I quoted the excerpt above ;-)

Thanks for the other two recommendations, though!

Christos
--



Wed, 20 Mar 2002 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. global/static vars inside loops

2. static global vars and functions

3. global vars vs: local vars

4. const global vars

5. Global vars versus pointers

6. how to access local and global vars in a function

7. global vs local vars

8. global Vars

9. Global vs. Local vars and .MAP

10. Accessing vars in global and module scope in ANSI C

11. Accessing vars in global and module scope in ANSI C

12. Accessing vars in global

 

 
Powered by phpBB® Forum Software