測試環境與編譯器版本
- 平台:Intel Core 2 Duo
- GCC (TDM-2 for MinGW) 4.3.0
- VC 9.0 (cl 15.00.21022.08)
- Intel C++ Compiler (icl 10.1)
- PGI (pgcc 7.16)
- CodeGear (bcc32 6.10)
測試程式:數值積分基準測試
來源於 Intel 編譯器範例,修改頭檔使各編譯器均可編譯。計算 |sin(x)| 在 [0, 2π] 積分,使用中點法則,內部矩形數從 2^2 到 2^26。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#define INTEG_FUNC(x) fabs(sin(x))
int main() {
unsigned int i, j, N;
double step, x_i, sum;
double start, finish, duration;
double interval_begin = 0.0;
double interval_end = 2.0 * 3.141592653589793238;
start = clock();
for (j = 2; j < 27; j++) {
N = 1 << j;
step = (interval_end - interval_begin) / N;
sum = INTEG_FUNC(interval_begin) * step / 2.0;
for (i = 1; i < N; i++) {
x_i = i * step;
sum += INTEG_FUNC(x_i) * step;
}
sum += INTEG_FUNC(interval_end) * step / 2.0;
printf("%10d | %14e\n", N, sum);
}
finish = clock();
duration = (finish - start);
printf("Application Clocks = %10e\n", duration);
return 0;
}
測試結果 (時鐘週期數,越低越好)
Intel Core 2 Duo - 無優化 (-O0 /Od)
| GCC | VC | Intel | PGI | CodeGear |
| 17161 | 14461 | 12441 | 10514 | 13400 |
| 17133 | 14430 | 11687 | 9956 | 12917 |
| 17155 | 14476 | 11871 | 10099 | 13026 |
通用優化 (-O2 /O2)
| GCC | VC | Intel | PGI | CodeGear |
| 13011 | 7737 | 4540 | 9348 | 12636 |
| 16571 | 7706 | 4185 | 9148 | 13026 |
| 16573 | 7706 | 4042 | 9183 | 13057 |
針對平台優化
| 選項 | GCC | VC | Intel | PGI |
| 指令 | -march=core2 -O2 | /arch:SSE2 /O2 | -QxT | -tp core2 -O2 |
| 結果 | 16060 | 7710 | 1938 | 9578 |
CodeGear 無對應平台優化選項。
其他平台驗證
Pentium 4 1.5GHz / Linux
| GCC | ICC | PGCC |
| -O2 | 24,920,000 | 10,840,000 | 22,270,000 |
| -O0 | 28,290,000 | 19,210,000 | 24,320,000 |
| 平台優化 | -march=pentium4 -O2 | -xN | -tp piv -O2 |
| 結果 | 24,990,000 | 6,640,000 | 22,150,000 |
Athlon X2 4800+ / Linux
| GCC | ICC | PGCC |
| -O0 | 9,390,000 | 14,950,000 | 9,950,000 |
| -O2 | 8,910,000 | 9,240,000 | 9,400,000 |
| 平台優化 | -march=amdfam10 -O2 | -msse3 -O2 | -tp k8-32 -O2 |
| 結果 | 8,800,000 | 3,800,000 | 9,030,000 |
結論與排名
- Intel C++ Compiler (icc/icl):數值計算效能最強,針對特定 CPU 優化提升極大,甚至在 AMD CPU 上配合正確選項 (
-msse3) 亦有顯著提升。
- Microsoft Visual C++ (cl):優化效果明顯,-O2 後大幅領先 GCC、PGI、CodeGear。
- PGI (pgcc):優化提升有限,AMD 平台表現中規中矩,可能未找到最佳選項組合。
- CodeGear (bcc32):優化幅度極小,表現接近無優化水平。
- GCC (MinGW):表現令人失望,-O2 提升不明顯,平台優化 (
-march=core2) 反而變慢,可能是版本或配置問題。
總結:在數值計算領域,「最快」的選擇屬於 Intel 編譯器。但需注意測試程式源自 Intel 範例,可能對 Intel 編譯器有利。實際專案選型還需考慮相容性、授權、跨平台需求等因素。