程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++編譯器性能比較

C++編譯器性能比較

編輯:關於C++

現在市面上,主流的C/C++編譯器包括M$的CL、gcc、Intel的icl、PGI的pgcc及Codegear的bcc(原來屬於Borland公司)。Windows上使用最多的自然是cl,而在更廣闊的平台上,gcc則是C/C++編譯器的首選。但要提到能力優化,排名就未必與它們的市場占有率一致了。

今天一時興起,便做了一個各編譯器數值性能的比較。測試的代碼是一個求積分的程序,來源於intel編譯器的例子程序,修改了一個頭文件,以便每個編譯器都能編譯。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

// Function to be integrated
// Define and prototype it here
// | sin(x) |
#define INTEG_FUNC(x) fabs(sin(x))

// Prototype timing function
double dclock(void);

int main(void)
{
// Loop counters and number of interior points
unsigned int i, j, N;
// Stepsize, independent variable x, and accumulated sum
double step, x_i, sum;
// Timing variables for evaluation
double start, finish, duration, clock_t;
// Start integral from
double interval_begin = 0.0;
// Complete integral at
double interval_end = 2.0 * 3.141592653589793238;

// Start timing for the entire application
start = clock();

printf(" \n");
printf(" Number of | Computed Integral | \n");
printf(" Interior Points | | \n");
for (j=2;j<27;j++)
{
printf("------------------------------------- \n");

// Compute the number of (internal rectangles + 1)
N = 1 << j;

// Compute stepsize for N-1 internal rectangles
step = (interval_end - interval_begin) / N;

// Approx. 1/2 area in first rectangle: f(x0) * [step/2]
sum = INTEG_FUNC(interval_begin) * step / 2.0;

// Apply midpoint rule:
// Given length = f(x), compute the area of the
// rectangle of width step
// Sum areas of internal rectangle: f(xi + step) * step

for (i=1;i<N;i++)
{
x_i = i * step;
sum += INTEG_FUNC(x_i) * step;
}

// Approx. 1/2 area in last rectangle: f(xN) * [step/2]
sum += INTEG_FUNC(interval_end) * step / 2.0;

printf(" %10d | %14e | \n", N, sum);
}
finish = clock();
duration = (finish - start);
printf(" \n");
printf(" Application Clocks = %10e \n", duration);
printf(" \n");

return 0;
}

當然,這個代碼來自於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

17161 14461 12441 10514 13400

17133 14430 11687 9956 12917

17155 14476 11871 10099 13026

編譯選項 -O2

13011 7737 4540 9348 12636

16571 7706 4185 9148 13026

16573 7706 4042 9183 13057

針對平台的優化

-march=core2 -O2 /arch:SSE2 /O2 -QxT -tp core2 -O2 無

16060 7710 1938 9578

測試的結果說明,在數值計算方法,intel的編譯器是非常利害的,特別是針對某CPU的優化,能提高很多性能。GCC表現卻有些讓人失望。在禁止優化到-O2級優化的對比中,可以看出intel與m$的編譯器的優化效果是非常明顯的,而其它編譯器優化後的提高非常有限。如果給個排名,那麼將是 icl>cl>pgcc>bcc>gcc.

另外,在一台P4 1.5G的機器,linux環境下,測試得到

gcc icc pgCC

-O2 -O2 -O2

24920000 10840000 22270000

-O0 -O0 -O0

28290000 19210000 24320000

-march=pentium4 -O2 -xN -tp piv -O2

24990000 6640000 22150000

同樣,還是intel的表現最好,而gcc最差。

又在Athlon X2 4800+, Linux上測試,得到下表

gcc icc pgcc

-O0 -O0 -O0

9390000 14950000 9950000

-O2 -O2 -O2

8910000 9240000 9400000

-march=amdfam10 -O2 -msse3 -O2 -tp k8-32 -O2

8800000 3800000 9030000

雖然icc主要是針對intel的處理器,但只要優化選項找對,同樣能帶給amd cpu性能的巨大提高。gcc也回歸到普通水平。奇怪的是pgi的編譯器,估計是我還沒找到好的選項吧。

總結看來,在數值計算方法,“最快”的選擇應該屬於intel.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved