gcc和llvm對程式碼的不同優化效果

lt發表於2017-03-29

程式碼1,p596b.cpp

#include <cstdio>
#include <cmath>
typedef long long ll;
const int n=1000;
ll fa(int r)
{
    ll sum2=0,sum3=0,sum4=0,b2,c2;
    for (int a =1; a<r; a++)
    {
        b2=sqrt(r*r - a*a);
        sum2=sum2+b2;
        for (int b =1; b<=b2; b++)
        {
            c2=sqrt(r*r - a*a - b*b);
            sum3=sum3+c2;
            for (int c=1; c<=c2; c++)
            {
                sum4=sum4+sqrt(r*r - a*a - b*b - c*c);
            }
        }
    }
    return 1+8*r+sum2*24+sum3*32+sum4*16;
}
int main()
{
    ll l=fa(n);
    printf("%lld",l);
}

程式碼2,p596c.cpp

#include <cstdio>
#include <cmath>
typedef long long ll;
const int n=1000;
ll fa(int r)
{
    ll sum2=0,sum3=0,sum4=0,b2,c2,b1,c1,a,b,c;
    for (a =1; a<r; a++)
    {
        b1=r*r - a*a;
        b2=sqrt(b1);
        sum2=sum2+b2;
        for (b =1; b<=b2; b++)
        {
            c1=b1 - b*b;
            c2=sqrt(c1);
            sum3=sum3+c2;
            for (c=1; c<=c2; c++)
            {
                sum4=sum4+sqrt(c1 - c*c);
            }
        }
    }
    return 1+8*r+sum2*24+sum3*32+sum4*16;
}
int main()
{
    ll l=fa(n);
    printf("%lld",l);
}

輸出

D:\>g++ p596b.cpp -O3

D:\>timer a
Timer 9.01 : Igor Pavlov : Public domain : 2009-05-31
4934805110729
Kernel Time  =     0.046 =    0%
User Time    =     7.519 =   97%
Process Time =     7.566 =   98%
Global Time  =     7.686 =  100%

D:\>g++ p596c.cpp -O3

D:\>timer a
Timer 9.01 : Igor Pavlov : Public domain : 2009-05-31
4934805110729
Kernel Time  =     0.327 =    1%
User Time    =    21.746 =   97%
Process Time =    22.074 =   99%
Global Time  =    22.192 =  100%

D:\>clang++ p596b.cpp -O3

D:\>timer a
Timer 9.01 : Igor Pavlov : Public domain : 2009-05-31
4934805110729
Kernel Time  =     0.031 =    0%
User Time    =    11.528 =   98%
Process Time =    11.559 =   98%
Global Time  =    11.686 =  100%

D:\>clang++ p596c.cpp -O3

D:\>timer a
Timer 9.01 : Igor Pavlov : Public domain : 2009-05-31
4934805110729
Kernel Time  =     0.000 =    0%
User Time    =    11.684 =   99%
Process Time =    11.684 =   99%
Global Time  =    11.780 =  100%

第1個程式碼在g++上比clang++上編譯,執行更快。
第2個程式碼本來想引入2個臨時變數,減少乘法運算,但在g++上起了反作用,在clang++上沒有作用。

相關文章