代码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++上没有作用。