There are several analysis tools to measure our program and give suggestions to improve performance.
Now, we will introduce two common-used tools: gprof and valgrind
- Gprof
Gprof is a profiling program which collects and arranges statistics on your programs. It will insert codes to head and tail of each functions to collect running information by sampling some time points. When program running is end, it will create a file namedgmon.out. And we can run profile tool to analysis occupied time of different functions.
- Add
-pgto your compiling file (e.g. CMake or Makefile) - Running your program
exe - Typing
gprof exein the command
There is another easy-to-use toolgprof2dotto visualize your analytics results. You can usegprof exe | gprof2dot | dot -Tpdf -o result.pdfto generate a pdf file to display the results.gprof2dotis a python tool, and you can usepipto install it.
- Valgrind
Valgrind was originally designed to be a free memory debugging tool for Linux on x86, but has since evolved to become a generic framework for creating dynamic analysis tools such as checkers and profilers. Callgrind is a competent of valgrind for profiling CPU usage. With Callgrind, you can look up the running time of different functions. Similarly, another tool Kcachegrind is useful to provide a visual interface for beginner. Fortunately, you don’t need to add extra compiling flags, and you just need following three steps:
- Compile your project as usual
- Running callgrind with
valgrind --tool=callgrind ./cpuload - kcachegrind profile.callgrind
Using these out-of-box tools will benefit to hint us to optimize our codes well. Besides, other performance library like libpapi need us to insert codes to our program, and it’s not good for programmability. For me, I prefer to use these performance profile tools to detect my codes.