Hello GNU profiling

The profiling tool in GNU is called gprof. Here is a short, boring example of how to use it.

1) Write hello world in C (hello.c)

<

pre lang=”c”>
#include <stdio.h>

int foo() {
int b = 54324;
int j;
for (j=0; j < 1000000; j++) {
b = b^j;
}
return b;
}

int main() {
int a = 321782;
int i;
for(i=0; i<1000; i++) {
a = a ^ foo();
}
printf(“Hello foo: %d\n”, a);
return 0;
}

}

2) Compile with -pg option

gcc -pg hello.c

3) Run the program to generate profiling information

./a.out # this generates gmon.out file

4) Run gprof on the program and read output in less:

gprof a.out gmon.out | less

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.