This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Global variables are slower?
- To: Anjul Srivastava <anjul dot srivastava at sanchez dot com>
- Subject: Re: Global variables are slower?
- From: Michael Meissner <meissner at cygnus dot com>
- Date: Fri, 11 Feb 2000 17:43:46 -0500
- Cc: "'gcc at gcc dot gnu dot org'" <gcc at gcc dot gnu dot org>
- References: <2473C7ACA21ED211854F08002BB768C40551D060@OZ>
On Fri, Feb 11, 2000 at 05:15:46PM -0500, Anjul Srivastava wrote:
> Hello folks,
>
> Somebody told me that accessing a global variable requires an extra
> instruction, and thus, global variables are slower than local variables.
>
> Is this true for gcc generated code? I'm surprised if it is because I
> understood from my knowledge of C that there was no performance difference
> between local and global variables.
It depends on the architecture, the implementation of the architecture, options
used, code, phase of the moon. In general, for modern x86 processors, it
should be mostly the same. For some 32-bit RISC processors, you need 2
instructions to reference arbitrary memory, though you can dedicate a register
to point into a common area for small globals, and use 1 instruction for those
values. For other processors, you might have to load up the address from a
table of addresses before you can reference global values. If you compile code
for any kind of shared library, accesses to globals will definately be slower.
> Can it be optimized out, at least for repeated accesses, if it is true?
GCC doesn't do the best job optimizing globals and statics.
--
Michael Meissner, Cygnus Solutions, a Red Hat company.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work: meissner@redhat.com phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org fax: +1 978-692-4482