# C++ Profiling with `gprof` ## What is `gprof`? `gprof` is a GNU profiler that helps analyze where a program spends most of its execution time. It provides function call counts and execution time details. ## Installing `gprof` ```sh $ sudo apt-get install binutils ``` ## Compiling a C++ Program for Profiling To use `gprof`, compile your program with the `-pg` flag: ```sh $ g++ -pg -o my_program my_program.cpp ``` ## Example C++ Program Create a file `my_program.cpp` with the following code: ```cpp #include #include #include void slowFunction() { std::this_thread::sleep_for(std::chrono::seconds(1)); } void fastFunction() { for (volatile int i = 0; i < 1000000; ++i); } int main() { for (int i = 0; i < 5; ++i) slowFunction(); for (int i = 100; i < 200; ++i) fastFunction(); return 0; } ``` ## Running and Profiling the Program 1. Compile the program: ```sh $ g++ -pg -o my_program my_program.cpp ``` 2. Execute the program to generate `gmon.out`: ```sh $ ./my_program ``` 3. Analyze the profiling data: ```sh $ gprof my_program gmon.out > profile.txt $ cat profile.txt ``` ## Understanding the Output - **Flat Profile**: Shows execution time spent in each function. - **Call Graph**: Displays function call relationships and their execution time. ## Best Practices for Using `gprof` - Use `-O2` optimizations but **avoid `-O3`**, which may inline functions and reduce profiling accuracy. - Profile with realistic input data to get meaningful results. - Optimize the slowest functions first based on the profiling report. ## Conclusion `gprof` is a powerful tool for detecting performance bottlenecks in C++ programs. By identifying expensive functions, developers can make targeted optimizations.