Created
April 8, 2018 06:18
-
-
Save tulik/03d03f7389bbb5755e6b0e8cf558ffe5 to your computer and use it in GitHub Desktop.
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 <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
void bench(void (*f)(int)); | |
void if_func_1(int i); | |
void if_func_2(int i); | |
void if_func_3(int i); | |
int main() { | |
bench(&if_func_1); | |
bench(&if_func_2); | |
bench(&if_func_3); | |
return 0; | |
} | |
void bench(void (*f)(int)) { | |
int i; | |
struct timespec start, end; | |
float delta_us; | |
clock_gettime(CLOCK_MONOTONIC_RAW, &start); | |
for (i = 2147483647; -2147483648 != i; i--) { | |
(*f)(i); | |
} | |
clock_gettime(CLOCK_MONOTONIC_RAW, &end); | |
delta_us = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) * 0.001; | |
printf("%.3fms\n", delta_us * 0.001); | |
} | |
void if_func_1(int i) { | |
if (0 == i) { | |
return; | |
} | |
if (1 == i) { | |
return; | |
} | |
if (2 == i) { | |
return; | |
} | |
if (3 == i) { | |
return; | |
} | |
return; | |
} | |
void if_func_2(int i) { | |
if (0 == i) { | |
return; | |
} else if (1 == i) { | |
return; | |
} else if (2 == i) { | |
return; | |
} else if (3 == i) { | |
return; | |
} | |
return; | |
} | |
void if_func_3(int i) { | |
if (0 == i || 1 == i || 2 == i || 3 == i) { | |
return; | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment