C Dialects


Double Precision


/* Ahl's simple benchmark */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main(void)
{
  double r,s,a;
  int i,n,iter=0;
  time_t t;

  printf("ahl.c -- The C version of Ahl's simple benchmark\n");
  srand(time(0));
  r=0; s=0;
  t=time(0);
  while (t==time(0));
  t=time(0);
  do {
    iter++;
    r=0; s=0;
    for (n=1;n<=100;n++) {
      a = n;
      for (i=1;i<=10;i++) {
        a = sqrt(a); 
        r = r + ((double)rand())/RAND_MAX;
      }
      for (i=1;i<=10;i++) {
        a = a*a; 
        r = r + ((double)rand())/RAND_MAX;
      }
      s = s + a;
    }
  } while ((time(0)-t)<20);
  printf("%d iterations\n",iter);
  printf("%g seconds per iteration\n",20./iter);
  printf("Accuracy %g\n",fabs(1010.-s/5.));
  printf("Random %f\n",fabs(1000.-r));
  return(0);
}

Single Precision


/* Ahl's simple benchmark */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

int main(void)
{
  float r,s,a;
  int i,n,iter=0;
  time_t t;

  printf("ahl.c -- The C version of Ahl's simple benchmark\n");
  srand(time(0));
  r=0; s=0;
  t=time(0);
  while (t==time(0));
  t=time(0);
  do {
    iter++;
    r=0; s=0;
    for (n=1;n<=100;n++) {
      a = n;
      for (i=1;i<=10;i++) {
        a = sqrt(a); 
        r = r + ((float)rand())/RAND_MAX;
      }
      for (i=1;i<=10;i++) {
        a = a*a; 
        r = r + ((float)rand())/RAND_MAX;
      }
      s = s + a;
    }
  } while ((time(0)-t)<20);
  printf("%d iterations\n",iter);
  printf("%g seconds per iteration\n",20./iter);
  printf("Accuracy %g\n",fabs(1010.-s/5.));
  printf("Random %f\n",fabs(1000.-r));
  return(0);
}


Copyright © 2001 Eric Korpela
korpela@ssl.berkeley.edu