|
写了一个小程序,是用Monte Carlo算法算圆周率的。
在VisualC++6.0 Release下跑500000000需要12秒。
在FC4下编译:
gcc aa.c -lm
然后执行,跑500000000却用了50多秒!
郁闷,是什么原因呢?难道Linux不如Windows吗?
附源程序:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
int main(int argc, char* argv[])
{
double x,y,sigma,pi=0.0;
long end=1,i,time1,time2,count;
while(end)
{
count=0;
printf("\nPlease input uplimit of the rand numbers( 0 to stop ): ");
scanf("%ld",&end);
if(end<=0)
{
printf("Error: Wrong Uplimit!\n");
continue;
}
printf("Now is calculating the pi ...\n");
time1=time(NULL);
srand(time1);
for(i=0;i<end;i++)
{
x=(double)rand()/RAND_MAX;
y=(double)rand()/RAND_MAX;
if((x*x+y*y)<=1.0) count++;
}
pi=(count/(double)end)*4;
sigma=sqrt(pi*(pi-1)/(double)end);
time2=time(NULL);
printf("Pi = %f +- %f\t\tTime use: %d seconds\n",pi,sigma,time2-time1);
}
return 0;
} |
|