How can I profile C++ code running in Linux?

C++Server Side ProgrammingProgramming

There are many great profiling tools for profiling C++ programs on Linux. The most widely used tool is Valgrind. It is a programming tool for memory debugging, memory leak detection, and profiling. You can use valgrind by passing the binary to it and setting the tool to callgrind. First generate the binary by compiling the program −

$ g++ -o hello.cpp hello
Now use valgrind to profile it:
$ valgrind --tool=callgrind ./hello

This will generate a file called callgrind.out.x. You can read this file using a tool called kcachegrind.

If you're using gcc, you can use the inbuilt profiling tool, gprof. You can use it while compiling the file as follows −

$ g++ -o hello hello.cpp -g -pg
Published on 28-Feb-2018 06:25:36