Gprof

From LQWiki
Jump to navigation Jump to search

Gprof allows for profiling a program, so you can analyze where your program passes most of its time in order to increase performance by modifying the code. The program must be compiled with a profiling option or available in the sourcecode. Then you will do a test run to collect profiling data. The results will be based on this test run.

Example

  • Write main.cpp:
#include <iostream>
int main()
{
  std::cout << "hello world" << std::endl;
}
  • Build it using the pg option:
gcc -Wall -c -pg main.cpp
gcc -Wall -lstdc++ -pg main.o
  • Call it so you get profile data from one run:
./a.out
  • Verify you have the profile file:
# ls gmon.out
gmon.out
  • Call gprof:
# gprof       
Flat profile:                    

Each sample counts as 0.01 seconds.
 no time accumulated               

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00      0.00     0.00        1     0.00     0.00  global constructors keyed to main                                                                         
  0.00      0.00     0.00        1     0.00     0.00  __static_initialization_and_destruction_0(int, int)                                                       
[...]

See also