Created
April 15, 2017 18:27
-
-
Save dgraham/8e65a407813df5b833ad31422a2a0ec9 to your computer and use it in GitHub Desktop.
Benchmark function pointer versus direct call.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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