diff --git a/C/pi.c b/C/pi.c index c1494f5567eecd161e7da4d00b066c2243e2cec5..7f873d8cd3c7f44189dc5a11d9dc263c9d546367 100644 --- a/C/pi.c +++ b/C/pi.c @@ -1,32 +1,30 @@ /* * pi.c * Neil Gershenfeld 2/6/11 -* calculation of pi by a scalar sum +* pi calculation benchmark * pi = 3.14159265358979323846 */ #include <stdio.h> -#include <sys/time.h> +#include <time.h> #define NPTS 1000000000 void main() { int i; - double a,b,c,pi,mflops; - unsigned long int start_time,end_time; - struct timeval start,end; + double a,b,c,pi,dt,mflops; + struct timespec tstart,tend; + clock_gettime(CLOCK_REALTIME,&tstart); a = 0.5; b = 0.75; c = 0.25; pi = 0; pi = 0; - gettimeofday(&start, NULL); for (i = 1; i <= NPTS; ++i) pi += a/((i-b)*(i-c)); - gettimeofday(&end, NULL); - start_time = start.tv_sec * 1e6 + start.tv_usec; - end_time = end.tv_sec * 1e6 + end.tv_usec; - mflops = NPTS*5.0/(end_time-start_time); + clock_gettime(CLOCK_REALTIME,&tend); + dt = (tend.tv_sec+tend.tv_nsec/1e9)-(tstart.tv_sec+tstart.tv_nsec/1e9); + mflops = NPTS*5.0/(dt*1e6); printf("NPTS = %d, pi = %f\n",NPTS,pi); - printf("time = %f, estimated MFlops = %f\n",(end_time-start_time)/1.0e6,mflops); + printf("time = %f, estimated MFlops = %f\n",dt,mflops); }