Calculating elements addresses from arrays once

Gruik gruikmusic@ifrance.com
Thu May 20 09:38:00 GMT 2004


Hello,
I was just wondering something.
Let's see my simplified code :
typedef struct _abc
{
   int a;
   int b;
   int c;
} abc;
void my_func ()
{
   abc my_array [50];
   //..further

   for (i = 0; i < n; i++)
   {
      if (my_array[i].a == value)
         my_array[i].b ++;
      //...
      my_array[i].c = my_array[i].b + 15;
      //other operations accessing fields of my_array[i]
   }
   //...
}
You can see my_array[i] is often accessed.
I know that in assembly (and so in machine code), to write in 
my_array[i], the computer has to :
1) Retrieve my_array address
2) Retrieve i value
3) Adding i to my_array address
4) Writing to the obtained address

My question is : do you think GCC, in this kind of loops, performs a 
little optimisation by saving somewhere the address of my_array[i] ?

If not, do you think the following code does a significant optimisation ?

void my_func ()
{
   abc * current_abc;
   for (current_abc = my_array, i = 0; i < n; current_abc++, i++)
   {
      if (current_abc->a == value)
         current_abc->b ++;
      //...
      current_abc->c = current_abc->b + 15;
      //other operations accessing fields of *current_abc
   }
   //...
}
Thank you
Gruik.
Note : in my real case, i have much more fields in my struct, and there 
are much more accesses to the current element. Using temporary variables 
should not be very efficient in my case.

Note2 :  it's for a PHP module and i want it to become the fastest possible

_____________________________________________________________________
Envie de discuter en "live" avec vos amis ? Télécharger MSN Messenger
http://www.ifrance.com/_reloc/m la 1ère messagerie instantanée de France



More information about the Gcc mailing list