Global variables are slower?

Anjul Srivastava anjul.srivastava@sanchez.com
Fri Feb 11 15:04:00 GMT 2000


Good point about the optimization suggested! I was talking about the best
optimization, i. e. transform the program to:

printf("882236160\n");

on the i686, and appropriately on other platforms.

Is it correct to summarize that if there are no function calls between
repeated references to a non-volatile global variable, then it can be
optimized to work as well as a local variable. Otherwise, global variables
require extra instructions for access?

Thanks all of you for the useful information.

Anjul.

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


On Fri, 11 Feb 2000 17:40:14 -0500, you wrote:

>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; }

In your example a compiler can deduce that i doesn't have to be updated
in memory until at least the call to printf(). The version of gcc I'm
using (2.95.2) doesn't do it, but it doesn't mean that compilers cannot
do it at all. This kind of optimization may or may not appear in future
versions of gcc depending on how important the gcc developers think this
kind of optimization is.



Dima


More information about the Gcc mailing list