数值计算C++编译器比较,欢迎讨论 (100分)

  • 主题发起人 主题发起人 kerbcurb
  • 开始时间 开始时间
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(n) > 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(n) > 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的测试结果
 
支 持BCBX
 
呵呵,楼主试过g++吗?
 
Dev-CPP用得C++编译器就是g++
 
单独看这个没用,还要看各个编译器打开了那些优化选项,
楼主可以用INTEL的C++编译器编译看看,然后用他的VTUNE
分析一下性能就知道哪个好了,目前公认的最好的是INTEL的
然后是VC,GCC,BORLAND最差。。。
 
to张无忌:
没法找到intel编译器的序列号或者注册文件,不知道你能否提供
Email:kerbcurb@yahoo.com.cn,另外,这几种编译器的优化选项,我不会设置,恳请行家介绍一下,分数我会增加。
谢谢关注
 
VC6.0的优化选项刚刚试验设置了一下
project/setting
1)general not using MFC
2)debug 无改变
3)C/C++
general Optimizations customize
code generation
processor pentium pro
single-thread
C++ language :Best-case always*
Optimizations customize
Assume no aliasing YES
favor small code YES
favor fast code YES
改变前后变化并不明显,很多时候比上面的结果要差
DEV-CPP的优化选项也进行了设置,未有明显改变


 
楼主试过BC++了么?watcom++听说也不错哦。呵啊。。。。。。。
 
license.lic(在[]里面的内容)for Intel c++ 编译器8
[FEATURE FComp INTEL 2099.0123 permanent uncounted 9A323AD799A4 /
HOSTID=ANY PLATFORMS="i86_n ia64_n" ISSUER="TEAM LAXiTY" /
NOTICE="If you like it, GO Buy it!" ck=132 SIGN=E1BC924C8C38
FEATURE FCompW INTEL 2099.0123 permanent uncounted D39FB275A79D /
HOSTID=ANY PLATFORMS="i86_n ia64_n" ISSUER="TEAM LAXiTY" /
NOTICE="If you like it, GO Buy it!" ck=114 SIGN=9486EA7E4440
FEATURE CComp INTEL 2099.0123 permanent uncounted FB055F8A8DB6 /
HOSTID=ANY PLATFORMS="i86_n ia64_n" ISSUER="TEAM LAXiTY" /
NOTICE="If you like it, GO Buy it!" ck=109 SIGN=25FA6562F3CC
FEATURE CCompW INTEL 2099.0123 permanent uncounted A7DC4916AD65 /
HOSTID=ANY PLATFORMS="i86_n ia64_n" ISSUER="TEAM LAXiTY" /
NOTICE="If you like it, GO Buy it!" ck=127 SIGN=AF988B54A6B2
]
 
据说watcom的编译器优化最好,我下载过,但是不能使用,那位朋友介绍一下设置
,BC++应该和BCB的区别不大,都是Borland编译器,感谢zjan521
 
intel70
0 0.99413520790898247 0.0058647920910175344
1 0.56392150893798842 0.43021369897099404
2 -0.31851497277221397 0.88243648171020239
3 0.17374700110460542 0.47847390385009747
4 0.090151948454353772 0.076118897902593866
5 0.096471176081350113 0.0051298698472683063
6 0.096264811234861955 0.00012621063012459299
7 0.096266783275956905 7.4940054162198066e-016
8 0.096266783275956322 5.9674487573602164e-016
9 0.09626678327596587 9.5340402239685318e-015
10 0.096266783275952811 1.2906342661267445e-014
11 0.096266783275983633 3.0614399904038692e-014
12 0.096266783276006088 2.2926105458509483e-014
13 0.096266783275861634 1.440930708085375e-013
14 0.096266783276077919 2.1403712135992237e-013
15 0.096266783275587881 4.86694018420053e-013
16 0.096266783275234719 3.6076697185194462e-013
17 0.096266783274971041 2.6931235019844735e-013
18 0.096266783274768175 2.0707047188039951e-013
19 0.096266783284985752 1.0214343260095404e-011
0.096266783275956322
3359030 - 3358739 = 291
 
楼住居然用DEBUG模式来测试性能,感觉不太合理,
当然是要把优化打开,最大化速度,然后把浮点优化打开,
同时还可以让他支持特定的CPU优化,如果是INTEL的编译器
还可以让他进行运行时优化,这样才合理一些。
 
1)在上面说的不是很明白,测试结果在VC下是release,我不常用VC,但是我感觉需要优化的选项,我都选了,但是有几个选项不能同时选,因为选了之后编译通不过。
2)intel编译器我刚装上,很不熟悉,更谈不上优化了。
3)不知道张无忌朋友有没有你说的VC和intel的开发环境,麻烦您贴出你的测试结果。
4)真诚感谢各位继续关注
 
既然不常用VC什么的,就不要搞对比测试,这样测试结论不准确,可能误道其他的程序员
我对这些单纯的测试没什么大兴趣,平时也很懒,所以就不好贴代码搞测试了,
 
我不能同意张无忌朋友的观点,更不想误导其他朋友,我对任何工具都没有偏见,因为我想让代码更快一些,所以贴了出来。再一个我也没有下什么结论,我贴出的只是结果。
我认为愿意使用何种工具是他自己的自由,每个爱好编成的朋友如何选择他自己的使用工具,决不是因为他听别人说哪个工具好或哪个不好,他有能力自己判断。
再次谢谢各位的关注!
 
代码改为Pascal后用Delphi7.0编译运行,结果如下
0 9.94135207908982E-0001 5.86479209101760E-0003
1 9.94135207908982E-0001 6.30197189465253E-0001
2 9.94135207908982E-0001 3.09563837709707E-0001
3 9.94135207908982E-0001 4.78473903850098E-0001
4 9.94135207908982E-0001 7.61188979025951E-0002
5 9.64711760813491E-0002 5.12986984726854E-0003
6 9.62648112348624E-0002 1.26210630123119E-0004
7 9.62667832759581E-0002 1.12485975395371E-0018
0.096266783275958120
4227 - 4217 = 10
过两天,我会贴出Fortran90的结果
 
Dev-CPP4.9.8.0
0 0.9941352079089824 0.005864792091017601
1 0.5639215089379882 0.4302136989709942
2 -0.3185149727722142 0.8824364817102025
3 0.1737470011046055 0.4784739038500978
4 0.09015194845435247 0.07611889790259527
5 0.0964711760813491 0.00512986984726858
6 0.09626481123486241 0.0001262106301231203
7 0.09626678327595808 6.776263578034403e-021
0.09626678327595808
2939446 - 2939436 = 10
第一帖的版本是4.9.6.0
BC++5.02
0 0.9941352079089823986 0.00586479209101760119
1 0.5639215089379882494 0.4302136989709941517
2 -0.3185149727722142338 0.8824364817102025027
3 0.1737470011046055118 0.4784739038500978592
4 0.09015194845435246842 0.07611889790259526722
5 0.09647117608134909919 0.005129869847268579524
6 0.09626481123486240718 0.0001262106301231204295
7 0.09626678327595808094 0
0.09626678327595808094
4331217 - 4331217 = 0
 
TO:张无忌,我看您大概只会耍嘴皮子吧。
 
我改成fortran 6.5运行,速度并不快,并且小数点后精确位数不高,我用的是doubleprecision数据类型,不知道是什么原因。
我感觉最快的是delphi的代码,输出精度也最高
 
看来很少有人从事数值计算方面的工作,准备揭帖,在结帖之前说明一下:
1)这是一个计算Bessel函数的,当然修改后也可以计算其他函数的积分,其中RI是Romberg积分。
2)计算用时的长短和具体的编译器对双精度浮点处理不同有关,我认为MS的C++编译器处理浮点数可能有问题,intel的好像为了兼容MS,在处理浮点数上与MS类似,但是Intel还有一套方案,Intel在帮助文件中有说明,说明中提到MS的编译器处理浮点数的方案接近IEEE的标准,注意这里是接近!
3)MS_VS_2003的编译器(大概就是经常提到的MS7.0)在这一问题上并没有改进。我作了实验,以下是计算伽玛(Gamma)函数的例子,这里只列出结果(如果你感兴趣,有关代码可以向我索取)
计算的是:Gamma(59)(58的阶乘:58!)
MS_VS_2003
2.3505613312828793 e+078
Dev-CPP4.9.8.10:
2.350561331282879 e+078
BCBX1.0:
2.350561331282878495 e+78
mathematica4.2:
2.3505613312828785718e+78
 
后退
顶部