Profiling your code is important if performance is of concern. This is especially true for performance-critical applications in scientific computing.
Profiling identifies those parts of the program that consume most of the resources, whether it is CPU time or memory, and which parts of the source code should receive the most attention and optimization efforts.
Several profiling tools are available on Linux for profiling applications written in C++. Here, we will focus on two major toolsets:
- Valgrind with Callgrind and KCachegrind, which includes a GUI.
- GProf, which is a terminal-based profiling tool
I will only address tools for a Linux/Unix environment. There are other tools available on Windows.
Table of Contents
Profiling using Valgrind, Callgrind, and KCachegrind
Valgrind is a versatile software suite that consists of various components for debugging and profiling software. The Callgrind component facilitates profiling.
We must first compile the application with debugging information. When using GCC for the compilation, we achieve this by using the
-g
option. The compiler will then embed debugging information into the executable.-
After compiling the program, we employ Valgrind's Callgrind tool to generate profiling data. The following command tells Valgrind to run our program using Callgrind:
valgrind --tool=callgrind [callgrind options] our_program [program options]
We can specify various options for Callgrind to control its behavior, as well as pass arguments to our program as needed.
Callgrind will generate a profiling data file named
callgrind.out.[pid]
, where[pid]
is the process ID of our program. This file contains detailed profiling information, which can be analyzed using other programs. -
We analyze the data with KCachegrind, which is a graphical user interface that presents the profiling information in a more accessible manner. To open the generated file in KCachegrind, we run
kcachegrind callgrind.out.[pid]
KCachegrind is a GUI tool to visually explore the data in the data file. It can visualize the call graph, identify how often functions have been called, and how much time was spent in each function.
Profiling using GProf
Another tool at our disposal is GProf, the GNU Profiler. It is a profiler included in the GNU Binutils suite. It generates a flat profile and call graph analysis of our program, which provides insights into how often functions are called and how much time is spent in each function. Unlike KCachegrind, it does not provide a GUI.
-
To use GProf, we need to compile our program with the
-pg
option, which enables profiling information in the executable. The-pg
flag must be used for both compilation and linking.Running our program will now automatically create a file named
gmon.out
, which contains the profiling data. -
We can analyze the data using GProf with the following command:
gprof our_program gmon.out
This command will output the profiling analysis to the terminal. Here, we can see the time spent in each function and the call relationships between functions.
-
Note that GProf does not offer a GUI but is only text-based. Its main application is as a tool that facilitates post-processing by scripts. One strong advantage over GUI tools the GProf output can easily be incorporated in an automated profiling workflow.
More information can be found in the official GProf documentation: http://sourceware.org/binutils/docs/gprof/
Top comments (0)