Quick guide to profile C/C++ source code with gprof
This article is just a quick reference for profiing C/C++ source code using gprof. C/C++ source code can be profiled using gprof in three steps.
- Compile the sources and link the program with profiling options “-pg”
bash# gcc -pg -g -c source1.c source2.c bash# gcc -pg -o myapp source1.o source2.o
- Execute the program to generate profiling output in gmon.out
bash# ./myapp
- Analyze the profiling output with gprof. It prints the list of functions which have taken more time during the execution of process. This list is in descending order so probably you would want to start optimizing the code in the same order.The output also shows how many times a function is invoked during the execution.
bash# gmon ./myapp gmon.out Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 33.34 0.02 0.02 7208 0.00 0.00 open 16.67 0.03 0.01 244 0.04 0.12 offtime 16.67 0.04 0.01 8 1.25 1.25 memccpy 16.67 0.05 0.01 7 1.43 1.43 write 16.67 0.06 0.01 mcount 0.00 0.06 0.00 236 0.00 0.00 tzset 0.00 0.06 0.00 192 0.00 0.00 tolower

