Global variables are slower?

Dima Volodin
Fri Feb 11 14:53:00 GMT 2000

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.


More information about the Gcc mailing list