This document provides guidelines and techniques for profiling C++ code. It covers three main approaches: using Valgrind's Callgrind with KCachegrind, inspecting generated assembly code with inline assembly comments, and using the utils/profiler C++ profiling class into your project.
1. Profiling with Valgrind Callgrind and KCachegrind
Valgrind's Callgrind is a powerful tool for profiling C++ applications. It helps you understand how your program executes, revealing call graphs, function call frequencies, and more.
Using Callgrind
- Run Your Program Under Callgrind: The following command executes your program while collecting detailed profiling information about function calls and CPU usage. Callgrind creates output files (typically named callgrind.out.<pid>) that contain the profiling data. These files can be examined to understand which parts of your code are consuming the most resources.
valgrind --tool=callgrind ./your_program
- Analyzing with KCachegrind Visualize the Data: Open the Callgrind output file using KCachegrind. This tool provides a graphical representation of the call graph, making it easier to identify hotspots and bottlenecks.
2. Checking Assembly Code and Using Assembly Comments
Sometimes it’s necessary to inspect the generated assembly code to understand how the compiler optimizes your code or to debug low-level issues.
- Generating Assembly Code CMake has targets built in for both assembler and preprocessor output. For a file called src.cpp, CMake generates the target src.s for assembler output and src.i for the preprocessor output. Use the following command for a list of targets
- Inserting Inline Assembly Comments Using asm Comments: Inline assembly comments can be used within your C++ code to help correlate the high-level code with the generated assembly. This is achieved by inserting a comment in the assembly output. The above statement uses the asm keyword to insert a comment into the generated assembly code. The string inside the asm call is treated as a comment (prefixed with #), making it easier to locate specific code segments when reviewing the assembly output.
asm("# This is an inline assembly comment: checking optimization");
3. Profiling with a the Profiler class
- Start and Stop Methods: The start method records the current time using std::chrono::high_resolution_clock::now() and associates it with a label. The stop method calculates the elapsed time since the corresponding start call and accumulates it in a time map. The profiler supports multiple measurements for the same label by accumulating the elapsed time, enabling you to profile code executed in loops or called multiple times.
doSomeWork();
static void stop(const char *label)
stop(label) when recording is on.
Definition profiler.hpp:90
static void start(const char *label)
start(label) when recording is on.
Definition profiler.hpp:84
- PROFILE macro: The PROFILE macro couples a start and stop with label the code passed as argument
#define PROFILE(func)
Time a single execution of func under the label #func using Profiler::start()/stop().
Definition profiler.hpp:44
- Printing the Results: The print static method outputs a neatly formatted table showing each label and its total execution time in milliseconds, sorted from the most time-consuming to the least.
- Conditional Compilation: The profiler’s functionality can be disabled in production builds by defining NPROFILE.