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

#define N 10000000
#define M 6

int cnt[N];
int sum = 0;

void func1()
{
  sum++;
}

void func2()
{
  sum--;
}

clock_t now(void)
{
  return clock();
}

double ms(clock_t t1, clock_t t2)
{
  return (t2 - t1) / CLOCKS_PER_SEC;
}

int main(void)
{
  void (*func[])(void) = { func1, func2 };
  int i, j;
  clock_t beg;
  
  for (i = 0; i < N; i++)
    cnt[i] = i % 2; // call altenatively

  puts("Call 0101010101....");
  beg = now();
  for (j = 0; j < M; j++)
    for (i = 0; i < N; i++)
      func[cnt[i]]();
  printf("%.3fms\n", ms(beg, now()));

  printf("sum = %d\n", sum);
  sum = 0;

  for (i = 0; i < N; i++)
    cnt[i] = (i % 3) ? 1 : 0; // call one in three times

  puts("Call 001001001....");
  beg = now();
  for (j = 0; j < M; j++)
    for (i = 0; i < N; i++)
      func[cnt[i]]();
  printf("%.3fms\n", ms(beg, now()));

  printf("sum = %d\n", sum);
  sum = 0;

  for (i = 0; i < N; i++)
    cnt[i] = ((i % 4) == 0 || (i % 4) == 1) ? 1 : 0;

  puts("Call 00110011....");
  beg = now();
  for (j = 0; j < M; j++)
    for (i = 0; i < N; i++)
      func[cnt[i]]();
  printf("%.3fms\n", ms(beg, now()));

  printf("sum = %d\n", sum);
  sum = 0;

  srand(time(NULL));
  for (i = 0; i < N; i++)
    cnt[i] = (rand() >> 3) % 2; // call in random

  puts("Call in Random");
  beg = now();
  for (j = 0; j < M; j++)
    for (i = 0; i < N; i++)
      func[cnt[i]]();
  printf("%.3fms\n", ms(beg, now()));

  printf("sum = %d\n", sum);

  return 0;
}
