Gprof, Valgrind, Gperf, etc

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 named gmon.out. And we can run profile tool to analysis occupied time of different functions.
  1. Add -pg to your compiling file (e.g. CMake or Makefile)
  2. Running your program exe
  3. Typing gprof exe in the command
    There is another easy-to-use tool gprof2dot to visualize your analytics results. You can use gprof exe | gprof2dot | dot -Tpdf -o result.pdf to generate a pdf file to display the results. gprof2dot is a python tool, and you can use pip to 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:
  1. Compile your project as usual
  2. Running callgrind with valgrind --tool=callgrind ./cpuload
  3. 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.