Profiling C/C++ with gprof

We will use the following flags:

  • -g for debugging information
  • -pg for profiling information

Add the -g -pg flags to the compiler (either gcc or g++):

 gcc -g -pg -o my_executable my_file.c

Then use gprof to generate the profile:

gprof my_executable > profile.txt

Review the profile with any text editor you wish:

geany profile.txt