Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Created September 8, 2012 14:51
Show Gist options
  • Save stepancheg/3675622 to your computer and use it in GitHub Desktop.
Save stepancheg/3675622 to your computer and use it in GitHub Desktop.
#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