Optimized program four times slower.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux