Archived

This topic is now archived and is closed to further replies.

knealeaj

global counters versus local

Recommended Posts

I am using static local variables for a number of counters in my game at the moment. I was wondering if using these or global variable counters was the most efficient way of coding. Is there any performnce issue ?? Thanks

Share this post


Link to post
Share on other sites
For integers and other primitive types, globals and static local variables compile to essentially the same assembly code. So unless for some reason you really need global access for the counters, I''d keep them as static locals.

Share this post


Link to post
Share on other sites
Is there any performnce issue ??

Your profiler should answer that question. You can''t learn without experimenting, you know.

Anyway, none that really matter: each time you call a function with a local static variable, the program checks whether it''s the first time you called the function, but since that happens only once, branch prediction should work nicely.

Share this post


Link to post
Share on other sites
Fruny that check exists only for non-POD types or POD types with non-constant initializers (and sometimes not even then).

Share this post


Link to post
Share on other sites
quote:
Original post by Ruudje
tip:
instead of int counter; use register int counter;

Compilers these days know when to use registers better than most programmers. (or so I''ve heard)

Share this post


Link to post
Share on other sites
Not only is register a useless keyword on modern compilers, it can''t be applied to global variables or local static variables. If you try it you''ll get an illegal storage specifier error for the global, and probably a multiple storage specifier error for the static.

Share this post


Link to post
Share on other sites