This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Optimized program four times slower.
- From: "Sebastien M." <ploouuf at yahoo dot fr>
- To: gcc-help at gcc dot gnu dot org
- Date: Sat, 14 Jul 2007 20:22:41 +0200
- Subject: Optimized program four times slower.
Hello,
I have a strange behaviour when I compile my program with -O2: it becomes slower.
Here is a simplified version :
// gcc version 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125)
//
// g++ -Wall -mno-cygwin perf_ptr.cpp -o perf_noopt
// g++ -Wall -O2 -mno-cygwin perf_ptr.cpp -o perf_opt
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
double empty(double x)
{
return x;
}
int main(int argc, char **argv)
{
double (*empty_ptr)(double) = empty;
long time;
int numtests = 200000000;
double x = 3.0;
time = clock();
for(int i=0;i<numtests;i++) empty(x);
printf("Time : empty = %ld\n", clock() - time);
time = clock();
for(int i=0;i<numtests;i++) empty_ptr(x);
printf("Time : empty_ptr = %ld\n", clock() - time);
return 0;
}
Result :
./perf_noopt
Time : empty = 1031
Time : empty_ptr = 1109
./perf_opt
Time : empty = 109
Time : empty_ptr = 4109
The loop which call "empty_ptr()" is four times slower with 02.
Is there a logical reason for this ?
Regards,
Sebastien