Created
June 28, 2012 14:43
-
-
Save xk/3011746 to your computer and use it in GitHub Desktop.
To call() or not to call() that is the question.
This file contains 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
//2012-06-28 functionCall.c jorge@jorgechamorro.com | |
/* | |
$ gcc -O0 /Users/jorge/Desktop/functionCall.c | |
$ ./a.out | |
without a function call: 2.789478 ns per iteration | |
with a function call : 3.574004 ns per iteration | |
With the function call it's 28.124473 percent slower than without the function call | |
*/ | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <sys/time.h> | |
static int flag= 0; | |
#define kLoops 1e9 | |
int f (int flag) { | |
if (flag) return 1; | |
else return 0; | |
} | |
long long now () { | |
struct timeval time; | |
gettimeofday(&time, NULL); | |
return (long long) ((time.tv_sec* 1000000) + time.tv_usec); | |
} | |
long long dsp (long long t, char* msg) { | |
t= now()- t; | |
printf("%s: %lf ns per iteration\n", msg, ((double)t)*1e3/kLoops); | |
fflush(stdout); | |
return t; | |
} | |
int main (int argc, char** argv) { | |
long long t, wfc, wofc; | |
long ctr, i; | |
ctr= 0; | |
i= kLoops; | |
t= now(); | |
while (i--) if (flag) f(flag); | |
wofc= dsp(t, "without a function call"); | |
ctr= 0; | |
i= kLoops; | |
t= now(); | |
while (i--) f(flag); | |
wfc= dsp(t, "with a function call "); | |
printf("\nWith the function call it's %lf percent slower than without the function call\n", ((double)wfc-(double)wofc)/((double)wofc/1e2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment