Tuesday, June 26, 2012

Profiling C++ application

Profiling allows you to learn where your program spent its time and which functions called which other functions while it was executing.

To profile C++ application I use GNU profiler.

To compile a source file for profiling, specify the -pg option when you run the compiler. 
g++ -pg -o bin/prog src/prog.cc
Once the program is compiled for profiling, you must run it in order to generate the information that gprof needs.
./bin/prog
After you have a profile data file gmon.out, you can run gprof to interpret the information in it.
gprof -b ./bin/prog
You will get such result:
 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  method1()
   0.00      0.00     0.00        1     0.00     0.00  method2()
   0.00      0.00     0.00        1     0.00     0.00  method3()
  
    Call graph
 
 
 granularity: each sample hit covers 4 byte(s) no time propagated
 
 index % time    self  children    called     name
                 0.00    0.00       1/1           main [4]
 [5]      0.0    0.00    0.00       1         method1() [5]
                 0.00    0.00       1/1           method2() [6]
 -----------------------------------------------
                 0.00    0.00       1/1           method1() [5]
 [6]      0.0    0.00    0.00       1         method2() [6]
                 0.00    0.00       1/1           method3() [7]
 -----------------------------------------------
                 0.00    0.00       1/1           method2() [6]
 [7]      0.0    0.00    0.00       1         method3() [7]
 -----------------------------------------------
  
 Index by function name
 
   [5] method1()               [6] method2()               [7] method3()
To read more about the GNU profiler go here.

No comments:

Post a Comment