/* memaccess.c
 *
 * Copyright (c) 2001
 * Hansjörg Malthaner
 *
 * Permission to use, copy, modify and distribute this software
 * for any purpose is hereby granted without fee, provided that 
 * the above copyright notice appears in all copies and that both
 * that copyright notice and this permission notice appear in 
 * supporting documentation.  
 *
 * I make no representations about the suitability of this software
 * for any purpose.  
 * It is provided "as is" without express or implied warranty.
 */

#include <stdlib.h>
#include <time.h>

int main(int argc, char **argv)
{
 
  char * base = calloc(10000000,1);
  long t0, t1, t2;
  int i;

  volatile char  const *p0 = base;
  volatile int   const *p1 = (int *)base;
  volatile short const *p2 = (short *)base;


  long t_loop10;
  long t_loop5;
  long t_loop25;

  long t_rand10;
  long t_rand5;
  long t_rand25;

  t0 = clock();
  for(i = 0; i<10000000; i++) {
    char *p; p++;
  }
  t1 = clock();

  t_loop10 = t1 - t0;

  t0 = clock();
  for(i = 0; i<2500000; i++) {
    char *p; p++;
  }
  t1 = clock();

  t_loop25 = t1 - t0;

  t0 = clock();
  for(i = 0; i<5000000; i++) {
    char *p; p++;
  }
  t1 = clock();

  t_loop5 = t1 - t0;


  t0 = clock();  
  for(i = 0; i<10000000; i++) {
    char *p; p++;
    random() % 10000000;
  }
  t1 = clock();

  t_rand10 = t1 - t0;

  t0 = clock();  
  for(i = 0; i<2500000; i++) {
    char *p; p++;
    random() % 2500000;
  }
  t1 = clock();

  t_rand25 = t1 - t0;

  t0 = clock();  
  for(i = 0; i<5000000; i++) {
    char *p; p++;
    random() % 5000000;
  }
  t1 = clock();

  t_rand5 = t1 - t0;

  printf("---------------- base times ----------------------\n"); 

  printf("Loop 10000000: %f sec\n", ((double)t_loop10)/CLOCKS_PER_SEC); 
  printf("Loop  5000000: %f sec\n", ((double)t_loop5)/CLOCKS_PER_SEC); 
  printf("Loop  2500000: %f sec\n", ((double)t_loop25)/CLOCKS_PER_SEC); 
  printf("Random loop 10000000: %f sec\n", ((double)t_rand10)/CLOCKS_PER_SEC); 
  printf("Random loop  5000000: %f sec\n", ((double)t_rand5)/CLOCKS_PER_SEC); 
  printf("Random loop  2500000: %f sec\n", ((double)t_rand25)/CLOCKS_PER_SEC); 

  printf("--------------- access times ---------------------\n"); 



  t0 = clock();  
  for(i = 0; i<10000000; i++) {
    *(p0+i);
  }
  t1 = clock();

  printf("Linear byte  read: %f MB/s\n", 10/(((double)t1-t0-t_loop10)/CLOCKS_PER_SEC)); 

  t0 = clock();
  for(i = 0; i<5000000; i++) {
    *(p2+i);
  }
  t1 = clock();

  printf("Linear short read: %f MB/s\n", 10/(((double)t1-t0-t_loop5)/CLOCKS_PER_SEC)); 


  t0 = clock();
  for(i = 0; i<2500000; i++) {
    *(p1+i);
  }
  t1 = clock();

  printf("Linear int   read: %f MB/s\n", 10/(((double)t1-t0-t_loop25)/CLOCKS_PER_SEC)); 


  t0 = clock();
  for(i = 0; i<10000000; i++) {
    *(p0+(random() % 10000000));
  }
  t1 = clock();

  printf("Random byte  read: %f MB/s\n", 10/(((double)t1-t0-t_rand10)/CLOCKS_PER_SEC)); 

  t0 = clock();
  for(i = 0; i<5000000; i++) {
    *(p2+(random() % 5000000));
  }
  t1 = clock();

  printf("Random short read: %f MB/s\n", 10/(((double)t1-t0-t_rand5)/CLOCKS_PER_SEC)); 

  t0 = clock();
  for(i = 0; i<2500000; i++) {
    *(p1+(random() % 2500000));
  }
  t1 = clock();

  printf("Random int   read: %f MB/s\n", 10/(((double)t1-t0-t_rand25)/CLOCKS_PER_SEC)); 




  free( base );
}
