Skip to content
Snippets Groups Projects
pi.c 650 B
Newer Older
  • Learn to ignore specific revisions
  • Neil Gershenfeld's avatar
    wip
    Neil Gershenfeld committed
    /*
    * pi.c
    * Neil Gershenfeld 2/6/11
    
    Neil Gershenfeld's avatar
    Neil Gershenfeld committed
    * pi calculation benchmark
    
    Neil Gershenfeld's avatar
    wip
    Neil Gershenfeld committed
    * pi = 3.14159265358979323846
    */
    
    #include <stdio.h>
    
    Neil Gershenfeld's avatar
    Neil Gershenfeld committed
    #include <time.h>
    
    Neil Gershenfeld's avatar
    wip
    Neil Gershenfeld committed
    
    #define NPTS 1000000000
    
    void main() {
       int i;
    
    Neil Gershenfeld's avatar
    Neil Gershenfeld committed
       double a,b,c,pi,dt,mflops;
       struct timespec tstart,tend;
       clock_gettime(CLOCK_REALTIME,&tstart);
    
    Neil Gershenfeld's avatar
    wip
    Neil Gershenfeld committed
       a = 0.5;
       b = 0.75;
       c = 0.25;
       pi = 0;
       for (i = 1; i <= NPTS; ++i)
          pi += a/((i-b)*(i-c));
    
    Neil Gershenfeld's avatar
    Neil Gershenfeld committed
       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);
    
    Neil Gershenfeld's avatar
    wip
    Neil Gershenfeld committed
       printf("NPTS = %d, pi = %f\n",NPTS,pi);
    
    Neil Gershenfeld's avatar
    Neil Gershenfeld committed
       printf("time = %f, estimated MFlops = %f\n",dt,mflops);
    
    Neil Gershenfeld's avatar
    wip
    Neil Gershenfeld committed
       }