This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Global variables are slower?



Thank you for your prompt responses Dima and Jeffrey.

Apparently the extra instruction(s) cannot optimized out on i686. This is
the simple overhead, in addition to the slowdown because the compiler cannot
optimize the cases listed by Jeffrey (dead assignment removal, etc.)

The program is (yes, I should have used volatile, but I am sure that gcc
cannot optimize this case to the extent a human can):

#include <stdio.h>

int i; int main() { int a,j; a=0;
for(j=0;j<1000;j++) for(i=0;i<1000000;i++)  a+=i;
printf("%d\n",a); return 0; }

time a.out (3 readings of each case)

Global optimized (gcc -O)
13.53user 0.01system 0:13.64elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
13.29user 0.02system 0:13.41elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
13.12user 0.01system 0:13.20elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k

Local optimized (gcc -O)
4.09user 0.00system 0:04.16elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
4.07user 0.00system 0:04.08elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k
4.07user 0.01system 0:04.14elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k

Global unoptimized
14.95user 0.04system 0:16.56elapsed 90%CPU (0avgtext+0avgdata 0maxresident)k
15.27user 0.00system 0:15.55elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
15.26user 0.01system 0:15.55elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k

Local unoptimized
14.61user 0.01system 0:14.81elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
14.70user 0.02system 0:14.87elapsed 98%CPU (0avgtext+0avgdata 0maxresident)k
14.68user 0.00system 0:14.77elapsed 99%CPU (0avgtext+0avgdata 0maxresident)k

Anjul.

-----Original Message-----
From: dvv@egcs.dvv.ru [mailto:dvv@egcs.dvv.ru]
Sent: Friday, February 11, 2000 5:26 PM
To: Anjul Srivastava
Cc: 'gcc@gcc.gnu.org'
Subject: Re: Global variables are slower?


On Fri, 11 Feb 2000 17:15:46 -0500, you wrote:

>Hello folks,
> 
>Somebody told me that accessing a global variable requires an extra
>instruction, and thus, global variables are slower than local variables.

It depends on the architecture you run your program on.

>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.

Your understanding of your knowledge of C is wrong. Or your knowledge of
C is wrong. Or both.

>Can it be optimized out, at least for repeated accesses, if it is true?

It can and it is.

>Anjul.

Dima

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]