几种C++编译器的性能比较

现在市面上,主流的C/C++编译器包括M$的CL、gcc、Intel的icl、PGI的pgcc及Codegear的bcc(原来属于Borland公司)。Windows上使用最多的自然是cl,而在更广阔的平台上,gcc则是C/C++编译器的首选。但要提到能力优化,排名就未必与它们的市场占有率一致了。

 

今天一时兴起,便做了一个各编译器数值性能的比较。测试的代码是一个求积分的程序,来源于intel编译器的例子程序,修改了一个头文件,以便每个编译器都能编译。

  1. #include <stdio.h>
  2. #include <stdlib.h> 
  3. #include <time.h> 
  4. #include <math.h>
  5. // Function to be integrated
  6. // Define and prototype it here
  7. // | sin(x) |
  8. #define INTEG_FUNC(x)  fabs(sin(x))
  9. // Prototype timing function
  10. double dclock(void);
  11. int main(void)
  12. {
  13.    // Loop counters and number of interior points
  14.    unsigned int i, j, N;
  15.    // Stepsize, independent variable x, and accumulated sum
  16.    double step, x_i, sum;
  17.    // Timing variables for evaluation   
  18.    double start, finish, duration, clock_t;
  19.    // Start integral from 
  20.    double interval_begin = 0.0;
  21.    // Complete integral at 
  22.    double interval_end = 2.0 * 3.141592653589793238;
  23.    // Start timing for the entire application
  24.    start = clock();
  25.    printf("     /n");
  26.    printf("    Number of    | Computed Integral | /n");
  27.    printf(" Interior Points |                   | /n");
  28.    for (j=2;j<27;j++)
  29.    {
  30.     printf("------------------------------------- /n");
  31.      // Compute the number of (internal rectangles + 1)
  32.      N =  1 << j;
  33.      // Compute stepsize for N-1 internal rectangles 
  34.      step = (interval_end - interval_begin) / N;
  35.      // Approx. 1/2 area in first rectangle: f(x0) * [step/2] 
  36.      sum = INTEG_FUNC(interval_begin) * step / 2.0;
  37.      // Apply midpoint rule:
  38.      // Given length = f(x), compute the area of the
  39.      // rectangle of width step
  40.      // Sum areas of internal rectangle: f(xi + step) * step 
  41.      for (i=1;i<N;i++)
  42.      {
  43.         x_i = i * step;
  44.         sum += INTEG_FUNC(x_i) * step;
  45.      }
  46.      // Approx. 1/2 area in last rectangle: f(xN) * [step/2] 
  47.      sum += INTEG_FUNC(interval_end) * step / 2.0;
  48.      printf(" %10d      |  %14e   | /n", N, sum);
  49.    }
  50.    finish = clock();
  51.    duration = (finish - start);
  52.    printf("     /n");
  53.    printf("   Application Clocks   = %10e  /n", duration);
  54.    printf("     /n");
  55.    return 0;
  56. }

当然,这个代码来自于intel,当然非常适合intel的编译器。以下的测试在Intel Core 2 Duo上进行。

 

gcc (GCC TDM-2 for MinGW) 4.3.0 VC 9.0 (cl 15.00.21022.08) Intel (icl 10.1) PGI (pgcc 7.16) CodeGear (bcc32 6.10) 
禁止优化
-O0/Od-Od-O0-Od
1716114461124411051413400
171331443011687995612917
1715514476118711009913026
编译选项 -O2
1301177374540934812636
1657177064185914813026
1657377064042918313057
针对平台的优化
-march=core2 -O2/arch:SSE2 /O2-QxT-tp core2 -O2
16060771019389578

 

测试的结果说明,在数值计算方法,intel的编译器是非常利害的,特别是针对某CPU的优化,能提高很多性能。GCC表现却有些让人失望。在禁止优化到-O2级优化的对比中,可以看出intel与m$的编译器的优化效果是非常明显的,而其它编译器优化后的提高非常有限。如果给个排名,那么将是 icl>cl>pgcc>bcc>gcc。

 

另外,在一台P4 1.5G的机器,linux环境下,测试得到

gcciccpgCC
-O2-O2-O2
249200001084000022270000
-O0-O0-O0
282900001921000024320000
-march=pentium4 -O2-xN-tp piv -O2
24990000664000022150000

 

同样,还是intel的表现最好,而gcc最差。

 

又在Athlon X2 4800+, Linux上测试,得到下表

gcciccpgcc
-O0-O0-O0
9390000149500009950000
-O2-O2-O2
891000092400009400000
-march=amdfam10 -O2-msse3 -O2-tp k8-32 -O2
880000038000009030000

 

虽然icc主要是针对intel的处理器,但只要优化选项找对,同样能带给amd cpu性能的巨大提高。gcc也回归到普通水平。奇怪的是pgi的编译器,估计是我还没找到好的选项吧。

 

总结看来,在数值计算方法,“最快”的选择应该属于intel。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值