A
A n g l e r
Hello everybody.
I've got such an aching me question concerning fast & well optimized
access to members of a structure (or class) and ways that VC compiler
handles it. Let's imagine the following structure and an array of its
type:
struct SSth1
{
int nMbr1, nMbr2, nMbr3;
} aSth1[1000];
now, when I access members within a loop, I can do the following:
for (int i=0; i<1000; i++)
{
for (int j=0; j<1000; j++)
{
doSth(j, &aSth1.nMbr1);
doSth(j, &aSth1.nMbr2);
doSth(j, &aSth1.nMbr3);
}
}
The doSth is just a method modifying the given above variables. Now, I
can also do the following:
for (int i=0; i<1000; i++)
{
int* nMbrLoc1=&aSth1.nMbr1;
int* nMbrLoc2=&aSth1.nMbr2;
int* nMbrLoc3=&aSth1.nMbr3;
for (int j=0; j<1000; j++)
{
doSth(j, nMbrLoc1);
doSth(j, nMbrLoc2);
doSth(j, nMbrLoc3);
}
}
I reckon that the second piece of code will be performed faster since
during execution of the code the pointers to the aSth1.nMbr1 will be
evaluated 1000 times rather than 1000*1000 times. Is that right? This is
what I assume. Alas, I do not know, how actually VC 2005 compiler
handles optimization issues like above. Can anyone elaborate a bit on
that? Do I have to follow that second way of coding, or, by any chance
compiler could handle pointer evaluation robustly on itself?
Cheers,
Peter.
I've got such an aching me question concerning fast & well optimized
access to members of a structure (or class) and ways that VC compiler
handles it. Let's imagine the following structure and an array of its
type:
struct SSth1
{
int nMbr1, nMbr2, nMbr3;
} aSth1[1000];
now, when I access members within a loop, I can do the following:
for (int i=0; i<1000; i++)
{
for (int j=0; j<1000; j++)
{
doSth(j, &aSth1.nMbr1);
doSth(j, &aSth1.nMbr2);
doSth(j, &aSth1.nMbr3);
}
}
The doSth is just a method modifying the given above variables. Now, I
can also do the following:
for (int i=0; i<1000; i++)
{
int* nMbrLoc1=&aSth1.nMbr1;
int* nMbrLoc2=&aSth1.nMbr2;
int* nMbrLoc3=&aSth1.nMbr3;
for (int j=0; j<1000; j++)
{
doSth(j, nMbrLoc1);
doSth(j, nMbrLoc2);
doSth(j, nMbrLoc3);
}
}
I reckon that the second piece of code will be performed faster since
during execution of the code the pointers to the aSth1.nMbr1 will be
evaluated 1000 times rather than 1000*1000 times. Is that right? This is
what I assume. Alas, I do not know, how actually VC 2005 compiler
handles optimization issues like above. Can anyone elaborate a bit on
that? Do I have to follow that second way of coding, or, by any chance
compiler could handle pointer evaluation robustly on itself?
Cheers,
Peter.