编译器性能比较
在网上看到一篇C++编译器性能比较的文章
http://www.zxbc.cn/html/20081119/67961.html
于是一时兴起,便把代码复制下来改了改,顺便改个了pascal的版本在自己机器上试了一下。
c++代码
#include <stdio.h>
#include <stdlib.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
#ifdef __cplusplus
extern "C" {
#endif
unsigned int __declspec(dllimport) __stdcall GetTickCount();
#ifdef __cplusplus
}
#endif
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
unsigned int 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 = GetTickCount();
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 = GetTickCount();
duration = (finish - start);
printf(" \n");
printf(" Application Clocks = %d ms \n", duration);
printf(" \n");
getchar();
return 0;
}
#include <stdlib.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
#ifdef __cplusplus
extern "C" {
#endif
unsigned int __declspec(dllimport) __stdcall GetTickCount();
#ifdef __cplusplus
}
#endif
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
unsigned int 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 = GetTickCount();
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 = GetTickCount();
duration = (finish - start);
printf(" \n");
printf(" Application Clocks = %d ms \n", duration);
printf(" \n");
getchar();
return 0;
}
pascal代码
program Test;
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils;
var
i, j, N: cardinal;
start, duration: cardinal;
step, x_i, sum: double;
interval_begin: double = 0.0;
interval_end: double = 2.0 * 3.141592653589793238;
begin
start := GetTickCount;
Writeln('');
Writeln('Number of | Computed Integral | ');
Writeln('Interior Points | | ');
for j := 2 to 26 do
begin
Writeln('------------------------------------- ');
N := 1 shl j;
step := (interval_end - interval_begin) / N;
sum := Abs(Sin(interval_begin)) * step / 2.0;
for i := 1 to N - 1 do
begin
x_i := i * step;
sum := sum + Abs(Sin(x_i)) * step;
end;
sum := sum + Abs(Sin(interval_end)) * step / 2.0;
Writeln(Format(' %10d | %14.7e | ', [N, sum]));
end;
duration := GetTickCount - start;
Writeln('');
Writeln(Format('Application Clocks = %d ms ', [duration]));
Writeln('');
Readln;
end.
{$APPTYPE CONSOLE}
uses
Windows,
SysUtils;
var
i, j, N: cardinal;
start, duration: cardinal;
step, x_i, sum: double;
interval_begin: double = 0.0;
interval_end: double = 2.0 * 3.141592653589793238;
begin
start := GetTickCount;
Writeln('');
Writeln('Number of | Computed Integral | ');
Writeln('Interior Points | | ');
for j := 2 to 26 do
begin
Writeln('------------------------------------- ');
N := 1 shl j;
step := (interval_end - interval_begin) / N;
sum := Abs(Sin(interval_begin)) * step / 2.0;
for i := 1 to N - 1 do
begin
x_i := i * step;
sum := sum + Abs(Sin(x_i)) * step;
end;
sum := sum + Abs(Sin(interval_end)) * step / 2.0;
Writeln(Format(' %10d | %14.7e | ', [N, sum]));
end;
duration := GetTickCount - start;
Writeln('');
Writeln(Format('Application Clocks = %d ms ', [duration]));
Writeln('');
Readln;
end.
我的机器环境是windows xp sp3 + intel core2 1.5G
C++编译器使用的是g++4.3.0-mingw32,c++builder 2009的bcc32,和vs2008的cl
pascal编译器使用的是开源的freepascal编译器fpc2.2.3和dephi 2009的dcc32
开启优化测试结果如下(单位:ms)
g++ 24531 24407 24390
bcc32 18078 17906 17938
cl 8922 8875 8875
fpc 12046 12015 12016
dcc32 13312 13313 13297
结果显示 cl>fpc>dcc32>bcc32>g++