gprof memento
(February 2006)
This is a sample usage of gprof, the GNU profiling tool (http://www.gnu.org/software/binutils/manual/gprof-2.9.1/).
First you need to compile and link with the -pg
option.
gcc -pg -o prog prog.c
When you execute the program prog
, a file named gmon.out
is generated. To read the content of this gmon.out
, you need to use the gprof
command.
gprof prog gmon.out > gmon.txt
Here is a sample prog.c
:
int glob_c = 0;
main() {
int i, j, x;
printf("Running...\n");
for (i=0; i<10; i++) {
for (j=0; j<14222; j++) {
x = f(i);
}
printf("x=%d\n", x);
}
printf("Done.\n");
}
int check_g(int y) {
if (y > 5) {
glob_c ++;
}
}
int f(int i) {
check_g(i);
if (i<=1) return 1;
return i*f(i-1);
}
And the gmon.txt
looks like:
[...]
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls us/call us/call name
46.15 0.12 0.12 142220 0.84 1.37 f
28.85 0.20 0.07 654212 0.11 0.11 check_g
25.00 0.26 0.07 main
[...]
granularity: each sample hit covers 4 byte(s) for 3.85% of 0.26 seconds
index % time self children called name
<spontaneous>
[1] 100.0 0.07 0.20 main [1]
0.12 0.07 142220/142220 f [2]
-----------------------------------------------
511992 f [2]
0.12 0.07 142220/142220 main [1]
[2] 75.0 0.12 0.07 142220+511992 f [2]
0.07 0.00 654212/654212 check_g [3]
511992 f [2]
-----------------------------------------------
0.07 0.00 654212/654212 f [2]
[3] 28.8 0.07 0.00 654212 check_g [3]
-----------------------------------------------
[...]
Index by function name
[3] check_g [2] f [1] main
[...]