K
kerbcurb
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是用于计算某函数的一段程序,各编译器的输出和计算时间比较。需要声明的的是,本贴不是用来评价开发工具好坏的,因为我不具备那样的水平,我只是想通过本贴学到如何优化编译器。希望高手指点。
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
const longdo
uble PI = 3.14159265358979323846264338327950;
longdo
uble BK( int n, longdo
uble theta, longdo
uble x )
{
if(n > 1)
return ( cos( x * sin( theta / n) - theta )) / (n * PI);
else
return ( cos( x * sin( theta ) - n * theta )) / PI;
}
//---------------------------------------------------------------------------
longdo
uble RI(longdo
uble (*Func)(int,longdo
uble,longdo
uble),int n,int m,longdo
uble a,longdo
uble b,longdo
uble x,longdo
uble tol)
{
int i,j;
longdo
uble s,R1[8],R2[8],temp,minerr = 1.0,prevalue,ERR;
longdo
uble h,err,p;
if(n < 2)n = 2;
if(n > 8) n = 8;
h = b - a;
int f,kk = 1,num = 0;
R1[0] = 0.5 * h * (( *Func)(m,a,x) + (*Func)(m,b,x));
while(true)
{
f = 1;
s = 0.0;
temp = a + h / 2.0;
while(temp < b)
{
s += (*Func)(m,temp,x);
temp += h;
}
R2[0] = (R1[0] + h * s) / 2.0;
for(j = 0;j < kk;j++)
{
f *= 4;
R2[j + 1] = R2[j] + (R2[j] - R1[j])/(f - 1);
}
err = fabs(R2[kk] - R1[kk - 1]);
cout<<num<<" "<<R2[kk]<<" "<<err<<endl;
if(err < minerr)
{
minerr = err;
prevalue = R2[kk];
}
ERR = fabs(prevalue) * tol;
h /= 2.0;
for(i = 0;i <= kk;i++)R1 = R2;
if(kk < n)kk++;
num++;
if(num > 18)break;
if(err < ERR)break;
}
return prevalue;
}
//---------------------------------------------------------------------------
longdo
uble B(int n,longdo
uble x)
{
longdo
uble temp;
if(abs > 1)
temp = RI(BK,3,n,0.0,n * PI,x,5.0e-14);
else
temp = RI(BK,3,n,0.0,PI,x,5.0e-14);
return temp;
}
//---------------------------------------------------------------------------
longdo
uble B(int n,longdo
uble x,longdo
uble tol)
{
longdo
uble temp;
if(abs > 1)
temp = RI(BK,3,n,0.0,n * PI,x,tol);
else
temp = RI(BK,3,n,0.0,PI,x,tol);
return temp;
}
//---------------------------------------------------------------------------
main()
{
cout.precision(20);
DWORD t1,t2;
t1 = GetTickCount();
cout<<B(0,25,1.0e-16)<<endl;
t2 = GetTickCount();
cout<<t2<<" - "<<t1<<" = "<<t2 - t1<<endl;
cin.get();
return 0;
}
//---------------------------------------------------------------------------
BCB6:
0 0.9941352079089823546 0.005899390791473262813
1 0.5639215089379872037 0.762896417590456899
2 -0.3185149727722142443 0.8824364817102013925
3 0.1737470011046062246 2.753854171917639704
4 0.09015194845435266224 0.8443400193523008524
5 0.0964711760813491831 0.05317515609991835257
6 0.0962648112348622742 0.001311077521519410892
7 0.09626678327595802886 8.832452998153161872e-16
8 0.09626678327595802886 8.716985466783455649e-17
0.09626678327595802886
3355765 - 3355745 = 20
//---------------------------------------------------------------------------
DEV-CPP:版本4.9.6.0
0 0.9941352079089824656 0.0058647920910175344034
1 0.56392150893798842493 0.43021369897099404067
2 -0.31851497277221396676 0.88243648171020239168
3 0.17374700110460541969 0.4784739038500974706
4 0.090151948454353730833 0.076118897902593907201
5 0.096471176081350085152 0.0051298698472683201821
6 0.096264811234861982769 0.00012621063012453748176
7 0.096266783275956904764 7.771561172376095783e-16
8 0.096266783275956321897 5.9674487573602164048e-16
9 0.096266783275965869815 9.5340402239685317909e-15
10 0.096266783275952810817 1.2906342661267444782e-14
11 0.096266783275983633383 3.0614399904038691602e-14
12 0.096266783276006059888 2.2898349882893853646e-14
13 0.096266783275861633751 1.4406531523292187558e-13
14 0.096266783276077919074 2.1403712135992236654e-13
15 0.096266783275587880508 4.8669401842005299841e-13
16 0.096266783275234718564 3.6076697185194461781e-13
17 0.096266783274971040596 2.6931235019844734779e-13
18 0.096266783274768175094 2.0707047188039950925e-13
19 0.096266783284985751878 1.0214343260095404275e-11
0.096266783275956321897
2146366 - 2145825 = 541
//---------------------------------------------------------------------------
VC6:
0 0.99413520790898247 0.0058647920910175344
1 0.56392150893798842 0.43021369897099404
2 -0.30472690274549208 0.89553676786916758
3 0.16627084635694764 0.41502670111061679
4 0.091341306234081765 0.048990371303452324
5 0.09639102186498652 0.0019878174244389868
6 0.096266783275957682 1.4016565685892601e-015
7 0.096266783275956919 8.4654505627668186e-016
8 0.096266783275956336 6.3837823915946501e-016
9 0.096266783275965717 9.3397511946591294e-015
10 0.096266783275953019 1.2115308756222021e-014
11 0.096266783275983162 2.9379276789143205e-014
12 0.096266783276005699 2.4369395390522186e-014
13 0.096266783275863882 1.4029055694919634e-013
14 0.096266783276074575 2.0192181260370035e-013
15 0.096266783275595486 4.6647408158406733e-013
16 0.096266783275240353 3.8428982218619012e-013
17 0.096266783274975246 2.8912983118800639e-013
18 0.096266783274771409 2.2190582704695316e-013
19 0.096266783284826157 1.0040884790285531e-011
0.096266783275956336
2036558 - 2036207 = 351
Press any key to continue
//---------------------------------------------------------------------------
BCBX1.0
0 1.994135207908982 0.005864792091017601 1.994135207908982e-16
1 1.563921508937988 0.4302136989709942 1.994135207908982e-16
2 0.6814850272277858 0.8824364817102025 1.994135207908982e-16
3 1.173747001104606 0.4784739038500978 1.994135207908982e-16
4 1.090151948454352 0.07611889790259527 1.994135207908982e-16
5 1.096471176081349 0.00512986984726858 1.096471176081349e-16
6 1.096264811234862 0.0001262106301231205 1.096264811234862e-16
7 1.096266783275958 1.084202172485504e-19 1.096266783275958e-16
0.09626678327595808
1810703 - 1810693 = 10
使用时间少到多比较:BCBX1.0 < BCB6.0 < VC6.0 < DEV-CPP,分别是10,20,230,13359
该函数Mathematica4.2的输出值为:0.0962667832759581161735033
需要说明的是本人并不对某些工具怀有偏见,因为仅此一例并不能说明问题,欢迎大家提供测试结果,欢迎提供VC7.1的测试结果
#include <iostream>
#include <math.h>
#include <windows.h>
using namespace std;
const longdo
uble PI = 3.14159265358979323846264338327950;
longdo
uble BK( int n, longdo
uble theta, longdo
uble x )
{
if(n > 1)
return ( cos( x * sin( theta / n) - theta )) / (n * PI);
else
return ( cos( x * sin( theta ) - n * theta )) / PI;
}
//---------------------------------------------------------------------------
longdo
uble RI(longdo
uble (*Func)(int,longdo
uble,longdo
uble),int n,int m,longdo
uble a,longdo
uble b,longdo
uble x,longdo
uble tol)
{
int i,j;
longdo
uble s,R1[8],R2[8],temp,minerr = 1.0,prevalue,ERR;
longdo
uble h,err,p;
if(n < 2)n = 2;
if(n > 8) n = 8;
h = b - a;
int f,kk = 1,num = 0;
R1[0] = 0.5 * h * (( *Func)(m,a,x) + (*Func)(m,b,x));
while(true)
{
f = 1;
s = 0.0;
temp = a + h / 2.0;
while(temp < b)
{
s += (*Func)(m,temp,x);
temp += h;
}
R2[0] = (R1[0] + h * s) / 2.0;
for(j = 0;j < kk;j++)
{
f *= 4;
R2[j + 1] = R2[j] + (R2[j] - R1[j])/(f - 1);
}
err = fabs(R2[kk] - R1[kk - 1]);
cout<<num<<" "<<R2[kk]<<" "<<err<<endl;
if(err < minerr)
{
minerr = err;
prevalue = R2[kk];
}
ERR = fabs(prevalue) * tol;
h /= 2.0;
for(i = 0;i <= kk;i++)R1 = R2;
if(kk < n)kk++;
num++;
if(num > 18)break;
if(err < ERR)break;
}
return prevalue;
}
//---------------------------------------------------------------------------
longdo
uble B(int n,longdo
uble x)
{
longdo
uble temp;
if(abs > 1)
temp = RI(BK,3,n,0.0,n * PI,x,5.0e-14);
else
temp = RI(BK,3,n,0.0,PI,x,5.0e-14);
return temp;
}
//---------------------------------------------------------------------------
longdo
uble B(int n,longdo
uble x,longdo
uble tol)
{
longdo
uble temp;
if(abs > 1)
temp = RI(BK,3,n,0.0,n * PI,x,tol);
else
temp = RI(BK,3,n,0.0,PI,x,tol);
return temp;
}
//---------------------------------------------------------------------------
main()
{
cout.precision(20);
DWORD t1,t2;
t1 = GetTickCount();
cout<<B(0,25,1.0e-16)<<endl;
t2 = GetTickCount();
cout<<t2<<" - "<<t1<<" = "<<t2 - t1<<endl;
cin.get();
return 0;
}
//---------------------------------------------------------------------------
BCB6:
0 0.9941352079089823546 0.005899390791473262813
1 0.5639215089379872037 0.762896417590456899
2 -0.3185149727722142443 0.8824364817102013925
3 0.1737470011046062246 2.753854171917639704
4 0.09015194845435266224 0.8443400193523008524
5 0.0964711760813491831 0.05317515609991835257
6 0.0962648112348622742 0.001311077521519410892
7 0.09626678327595802886 8.832452998153161872e-16
8 0.09626678327595802886 8.716985466783455649e-17
0.09626678327595802886
3355765 - 3355745 = 20
//---------------------------------------------------------------------------
DEV-CPP:版本4.9.6.0
0 0.9941352079089824656 0.0058647920910175344034
1 0.56392150893798842493 0.43021369897099404067
2 -0.31851497277221396676 0.88243648171020239168
3 0.17374700110460541969 0.4784739038500974706
4 0.090151948454353730833 0.076118897902593907201
5 0.096471176081350085152 0.0051298698472683201821
6 0.096264811234861982769 0.00012621063012453748176
7 0.096266783275956904764 7.771561172376095783e-16
8 0.096266783275956321897 5.9674487573602164048e-16
9 0.096266783275965869815 9.5340402239685317909e-15
10 0.096266783275952810817 1.2906342661267444782e-14
11 0.096266783275983633383 3.0614399904038691602e-14
12 0.096266783276006059888 2.2898349882893853646e-14
13 0.096266783275861633751 1.4406531523292187558e-13
14 0.096266783276077919074 2.1403712135992236654e-13
15 0.096266783275587880508 4.8669401842005299841e-13
16 0.096266783275234718564 3.6076697185194461781e-13
17 0.096266783274971040596 2.6931235019844734779e-13
18 0.096266783274768175094 2.0707047188039950925e-13
19 0.096266783284985751878 1.0214343260095404275e-11
0.096266783275956321897
2146366 - 2145825 = 541
//---------------------------------------------------------------------------
VC6:
0 0.99413520790898247 0.0058647920910175344
1 0.56392150893798842 0.43021369897099404
2 -0.30472690274549208 0.89553676786916758
3 0.16627084635694764 0.41502670111061679
4 0.091341306234081765 0.048990371303452324
5 0.09639102186498652 0.0019878174244389868
6 0.096266783275957682 1.4016565685892601e-015
7 0.096266783275956919 8.4654505627668186e-016
8 0.096266783275956336 6.3837823915946501e-016
9 0.096266783275965717 9.3397511946591294e-015
10 0.096266783275953019 1.2115308756222021e-014
11 0.096266783275983162 2.9379276789143205e-014
12 0.096266783276005699 2.4369395390522186e-014
13 0.096266783275863882 1.4029055694919634e-013
14 0.096266783276074575 2.0192181260370035e-013
15 0.096266783275595486 4.6647408158406733e-013
16 0.096266783275240353 3.8428982218619012e-013
17 0.096266783274975246 2.8912983118800639e-013
18 0.096266783274771409 2.2190582704695316e-013
19 0.096266783284826157 1.0040884790285531e-011
0.096266783275956336
2036558 - 2036207 = 351
Press any key to continue
//---------------------------------------------------------------------------
BCBX1.0
0 1.994135207908982 0.005864792091017601 1.994135207908982e-16
1 1.563921508937988 0.4302136989709942 1.994135207908982e-16
2 0.6814850272277858 0.8824364817102025 1.994135207908982e-16
3 1.173747001104606 0.4784739038500978 1.994135207908982e-16
4 1.090151948454352 0.07611889790259527 1.994135207908982e-16
5 1.096471176081349 0.00512986984726858 1.096471176081349e-16
6 1.096264811234862 0.0001262106301231205 1.096264811234862e-16
7 1.096266783275958 1.084202172485504e-19 1.096266783275958e-16
0.09626678327595808
1810703 - 1810693 = 10
使用时间少到多比较:BCBX1.0 < BCB6.0 < VC6.0 < DEV-CPP,分别是10,20,230,13359
该函数Mathematica4.2的输出值为:0.0962667832759581161735033
需要说明的是本人并不对某些工具怀有偏见,因为仅此一例并不能说明问题,欢迎大家提供测试结果,欢迎提供VC7.1的测试结果