This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
optimisation question
- From: "Remy X.O. Martin" <vsxo at hotmail dot com>
- To: gcc at gcc dot gnu dot org
- Date: Mon, 31 Jan 2005 15:26:59 +0100
- Subject: optimisation question
Hello,
I spent some time with Apple's Shark yesterday, profiling some of my code (rather, running a particular batch). Shark is a tool which samples running applications, and then allows one to find the most expensive expressions, giving hardware-specific suggestions on how to improve performance. I got 3 suggestions:
1) use a specific compiler option (-mdynamic-no-pic)
2) statements like a= c= d; are not (necessarily) optimal
3) Rewrite
for( i= 0; i< dest->N; i++ ) dest->array[i]= SOME_EXPRESSION;
as (e.g.)
for( i= 0, N=dest->N, array=dest->array; i< N; i++ ) array[i]= SOME_EXPRESSION;
I did what was suggested (2 & 3 only in around the flagged hotspot lines). I was quite surprised to see that this gave me a consistent performance gain of almost 10%, and not just on the profiled batch. It seems largely due to 1), but 2) and 3) certainly contribute. BTW, this is with gcc-3.4.3.
As far as I remember, I have been taught (like 15 years ago) that writing a=b=c can be better than a=c; b=c; because the compiler could more easily use values in registers. Similarly, 5 years ago I would have written the loop in 3) as
for(i=0, N=dest->N, array=dest->array; i<N; i++) *array++= SOME_EXPRESSION;
to avoid derefencing dest more often than necessary, and to have optimal access to dest->array.
I *thought* that this sort of (mild) obfuscation (esp. 3)) was no longer necessary, that is, that advanced compilers like gcc are now capable of recognising the situation, and generate highly comparable code in both cases.
Was that assumption wrong or premature (or does it depend on SOME_EXPRESSION for case 3))? FWIW: I do use -funroll-loops but not -funroll-all-loops.
Thanks,
R.