/* looptest.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 <time.h>

#define LOOPS 10000000

int main() {

  clock_t t0, t1, te;
  int i;

  t0 = clock();

  i=0;
  do {
    i++;
  } while(i<LOOPS);


  t1 = clock();

  te = t1-t0;

  printf("Empty test loop took %f seconds\n", ((double)te)/CLOCKS_PER_SEC);



  t0 = clock();

  i=0;
  do {
    int n;
    int arr[3];
    for(n=0; n<3; n++) {
      arr[n] = n;
    }

    i++;
  } while(i<LOOPS);


  t1 = clock();

  printf("Loop took %f seconds\n", ((double)(t1-t0)-te)/CLOCKS_PER_SEC);

  t0 = clock();

  i=0;
  do {
    int n=0;
    int arr[3];

    arr[n] = n;
    n++;
    arr[n] = n;
    n++;
    arr[n] = n;

    i++;
  } while(i<LOOPS);


  t1 = clock();



  printf("Unrolled loop took %f seconds\n", ((double)(t1-t0)-te)/CLOCKS_PER_SEC);


  t0 = clock();

  i=0;
  do {
    int n;
    int arr[40];
    int *src = arr;
    int *dst = arr;
    for(n=0; n<40; n++) {
      *src++ = *dst++;
    }

    i++;
  } while(i<LOOPS);


  t1 = clock();

  printf("Loop took %f seconds\n", ((double)(t1-t0)-te)/CLOCKS_PER_SEC);

  t0 = clock();

  i=0;
  do {
    int n=0;
    int arr[40];
    int *src = arr;
    int *dst = arr;
    for(n=0; n<40; n+=4) {
      *src++ = *dst++;
      *src++ = *dst++;
      *src++ = *dst++;
      *src++ = *dst++;
    }


    i++;
  } while(i<LOOPS);


  t1 = clock();

  printf("Partly unrolled loop took %f seconds\n", ((double)(t1-t0)-te)/CLOCKS_PER_SEC);

}
