global vs local vars 
Author Message
 global vs local vars

Is there any difference in using local instead of global variables in
time-critical functions which make use of a variable repeatedly in a while
loop? If I declare both as register variables, can I expect both to
provide the same performance? For example:

register int var;

void func() {
  /* use var here */

Quote:
}

VS

void func() {
  register int var;
  /* use var here */

Quote:
}

If possible, can anyone give a low-level explanation of the difference
between both options.

Thanks,
Marc Tardif



Sat, 28 Dec 2002 03:00:00 GMT  
 global vs local vars
On Tue, 11 Jul 2000 16:55:52 -0400, Marc Tardif

Quote:
> Is there any difference in using local instead of global variables in
> time-critical functions which make use of a variable repeatedly in a while
> loop? If I declare both as register variables, can I expect both to
> provide the same performance? For example:

> register int var;

> void func() {
>   /* use var here */
> }

> VS

> void func() {
>   register int var;
>   /* use var here */
> }

> If possible, can anyone give a low-level explanation of the difference
> between both options.

> Thanks,
> Marc Tardif

The answer is entirely compiler and processor hardware specific.  In
some cases the global variable will be faster, especially with older
and/or smaller processor architectures like many 8 bit processors.  In
other cases the local variable will be faster.  In still others there
will be no speed difference.

Again depending on the compiler or architecture, adding the register
keyword may or may not make a difference.

There are two ways to tell for sure with your particular
compiler/platform combination:

1.  View an assembly language listing of the generated code and check
against the processor timing specs.

2.  Run tests and time them.

Jack Klein
--
Home: http://jackklein.home.att.net



Sun, 29 Dec 2002 03:00:00 GMT  
 global vs local vars

Quote:
> Is there any difference in using local instead of global variables in
> time-critical functions which make use of a variable repeatedly in a while
> loop? If I declare both as register variables, can I expect both to
> provide the same performance? For example:

> register int var;

> void func() {
>   /* use var here */
> }

> VS

> void func() {
>   register int var;
>   /* use var here */
> }

You could concievably save having to reload the register
with the global, but it would depend on the exact code and
how the implementation did things (and you can't have static
register variables because you can only have one storage
class).

Quote:
> If possible, can anyone give a low-level explanation of the difference
> between both options.

That would be extremely system dependant. With a lot of
register variables, globals might win out because they get
allocated first, and you may run out of registers. Compile
to assembler if you're really interested on how your
compiler does things.

--
Craig Franck

Dover, NH



Sun, 29 Dec 2002 03:00:00 GMT  
 global vs local vars


Quote:
> Is there any difference in using local instead of global variables in
> time-critical functions which make use of a variable repeatedly in a while
> loop? If I declare both as register variables, can I expect both to
> provide the same performance? For example:

> register int var;

> void func() {
>   /* use var here */
> }

> VS

> void func() {
>   register int var;
>   /* use var here */
> }

> If possible, can anyone give a low-level explanation of the difference
> between both options.

Well, as others have correctly pointed out, there are systems where globals
are consistantly faster, systems where automatics are consistantly faster,
and systems where is really doesn't make that much difference (and that
doesn't even count the systems where one is faster in one situation, and
slower in another).

However, one thing to notice is that automatics are considerably more
optimizer friendly.  If you have an automatic which you never take the
address of, then the compiler can immediately conclude that that variable
cannot be aliased, that is, you cannot have a pointer to it.  That means
that the compiler can know that it knows all references to that variable
(both references that can modify it, and references which examine the
current value), and it can generate code based on that assumption.  Real
optimizers will notice this, and take advantage of this.

In contrast, it is considerably harder for the compiler to do that for a
global/static.  If it is a global, then it would have to examine all source
files for references to that variable.  If it is a static, then it would
have to reference that source file/function.  In either case, it is much
more work, and it is correspondingly less likely that an optimizer will
attempt it.

For a real (and only slightly contrived) instance, consider the code:

void func( const int *array, int count ) {
   auto int var = 0;   /* auto is here to emphesize that this is an
automatic */
   while (count--)
      var += *array++;
   /* do something with var */

Quote:
}

In the above code, the compiler is free to stick var into a register, and
accumulate things in a fast loop.  If you make var a global, then the
compiler may find it difficult to prove that array doesn't point to var, and
so will likely to generate code to read/write var for every loop iteration.

In addition, automatics are usually prefered (as in, sure, your routine
doesn't need to be reenterent now, but in the future...), and so, unless you
have a serious reason to suspect globals will be considerably faster, I'd go
with automatics.

--
poncho



Sun, 29 Dec 2002 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. global vars vs: local vars

2. Global vs. Local vars and .MAP

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

4. static local variables vs global variables

5. ? global vs local structures...

6. global vs local

7. Watching global vs. local variable w/ same name

8. global vs. local

9. global vs local states

10. basic question about pointers and local vars

11. VC6 SP3 debugger, local vars not recognized

12. global/static vars inside loops

 

 
Powered by phpBB® Forum Software