Created
September 8, 2012 14:51
-
-
Save stepancheg/3675622 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 <execinfo.h> | |
#include <sys/time.h> | |
#include <stdio.h> | |
void foo(int i) { | |
if (i == 0) { | |
void* array[128]; | |
backtrace(array, 128); | |
} else { | |
foo(i - 1); | |
} | |
} | |
long now_microseconds() { | |
struct timeval tv; | |
if (gettimeofday(&tv, 0) < 0) { | |
perror("gettimeofday"); | |
} | |
return tv.tv_sec * 1000000L + tv.tv_usec; | |
} | |
// compile with -O0 | |
int main(int argc, char** argv) { | |
for (;;) { | |
long start = now_microseconds(); | |
for (int i = 0; i < 1000; ++i) { | |
// stack of depth 100 | |
foo(100); | |
} | |
long end = now_microseconds(); | |
long dnanosecond = end - start; | |
printf("%ldns per backtrace call\n", dnanosecond); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment