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

In Linux platform there are many great profiling tools for profiling C++ programs. Valgrind is one of them. It is widely used. It is a programming tool for memory debugging, memory leak detection, and profiling. We 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 abc.cpp abc

Now use valgrind to profile it

$ valgrind --tool=callgrind ./abc

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 abc abc.cpp -g -pg