GNU gprof
Using the profiler gprof
// -- $ mkdir src; vim src/hello.c --
#include <stdio.h>
static void function2(int i) {
printf("%d\n", i);
}
static int function1(int x) {
int i;
for (i = 0; i < x; i++) {
function2(x);
}
return x + 1;
}
int main(void) {
int i, total;
total = 0;
for (i = 0; i < 5000; i++) {
total = function1(i);
function2(total);
}
return 0;
}
// -- $ vim makefile --
all: hello.prof
cat hello.time
cat hello.prof
hello.prof: gmon.out
gprof -b hello gmon.out > hello.prof
gmon.out: hello
/usr/bin/time -f "real %e\nuser %U\nsys %S\nCPU %P\n" -o hello.time ./hello > /dev/null
hello: src/hello.c
gcc -Wall -pg -o hello ./src/hello.c
clean:
rm -f hello*
rm -f gmon.out
// -- make --
$ make all
gcc -Wall -pg -o hello ./src/hello.c
/usr/bin/time -f "real %e\nuser %U\nsys %S\nCPU %P\n" -o hello.time ./hello > /dev/null
gprof -b hello gmon.out > hello.prof
cat hello.time
real 1.49
user 1.47
sys 0.00
CPU 99%
cat hello.prof
Flat profile:
Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls us/call us/call name
73.53 0.08 0.08 5000 16.18 16.18 function1
27.57 0.11 0.03 frame_dummy
0.00 0.11 0.00 12502500 0.00 0.00 function2
Call graph
granularity: each sample hit covers 2 byte(s) for 8.99% of 0.11 seconds
index % time self children called name
0.08 0.00 5000/5000 main [2]
[1] 72.7 0.08 0.00 5000 function1 [1]
0.00 0.00 12497500/12502500 function2 [4]
...
沒有留言:
張貼留言