Skip to content

Instantly share code, notes, and snippets.

@buchgr
Created November 30, 2014 00:27
Show Gist options
  • Select an option

  • Save buchgr/2770cf0a0604fc9bd4ff to your computer and use it in GitHub Desktop.

Select an option

Save buchgr/2770cf0a0604fc9bd4ff to your computer and use it in GitHub Desktop.
// gcc callbench.c -o callbench -O1
#include <stdio.h>
#include <stdlib.h>
#include "cycle.h"
int f0( int x )
{
/* The details of this calculation are not important */
return x;
}
int f1( int x) {
return f0 ( f0 ( x));
}
int f2( int x) {
return f1 ( f1 ( x));
}
int f3( int x) {
return f2 ( f2 ( x));
}
int f4( int x) {
return f3 ( f3 ( x));
}
int f5( int x) {
return f4 ( f4 ( x));
}
int f6( int x) {
return f5 ( f5 ( x));
}
int f7( int x) {
return f6 ( f6 ( x));
}
int f8( int x) {
return f7 ( f7 ( x));
}
int f9( int x) {
return f8 ( f8 ( x));
}
int f10( int x) {
return f9 ( f9 ( x));
}
int f11( int x) {
return f10 ( f10 ( x));
}
int f12( int x) {
return f11 ( f11 ( x));
}
int f13( int x) {
return f12 ( f12 ( x));
}
int f14( int x) {
return f13 ( f13 ( x));
}
int f15( int x) {
return f14 ( f14 ( x));
}
int f16( int x) {
return f15 ( f15( x));
}
int main (int argc, char *argv[]) {
int iterations = atoi(argv[1]);
ticks start = getticks();
int result = 0;
int i = iterations;
while (i --> 0) {
result += f16 (i);
}
ticks end = getticks();
double total = elapsed(end, start);
/**
* f16 will be called once, f15 twice, f14 four times, ... f0 2^16 times
* so the total number of function calls is 2^17 - 1.
*/
double percall = total / ( (1 << 17) - 1) / iterations;
printf("total cycles: %.2f, cycles per call: %.2f\n", total, percall);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment