Skip to content

Instantly share code, notes, and snippets.

@dgraham
Created April 15, 2017 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgraham/8e65a407813df5b833ad31422a2a0ec9 to your computer and use it in GitHub Desktop.
Save dgraham/8e65a407813df5b833ad31422a2a0ec9 to your computer and use it in GitHub Desktop.
Benchmark function pointer versus direct call.
#include <stdio.h>
#define MAX 100000000
int calculate() {
int sum = 0;
for (int i = 0; i < 10; i++) {
sum += i;
}
return sum;
}
long direct() {
long total = 0;
for (int i = 0; i < MAX ; i++) {
total += calculate();
}
return total;
}
long pointer(int (*sum)()) {
long total = 0;
for (int i = 0; i < MAX; i++) {
total += sum();
}
return total;
}
int main() {
printf("total: %ld\n", direct());
printf("total: %ld\n", pointer(calculate));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment