Wednesday, August 31, 2016

Optimize a C program by visually checking the profiling data

You need '''' (specific standalone tool) and the regular package ''dot'' (from graphviz).

g++ -pg -g myproc.cpp myproc
myproc myarg1 myarg2
gprof myproc | -s | dot -Tpng -o profiler.png

Note that the ''-g'' debugging option is required.

Note also that any code optimization flag, like ''-o3'' (aggressive speed optimization), will significantly impact the code layout, and it will be much harder to recognize the profiled sections.  It is much easier to read when most optimizations are tuned off (''-o1'' or ''-o0'').

But there is a nasty trade-off... Without optimization, the overall program will certainly behave differently than in production. It will not spend the same time in the same routines. Reciprocally, with full optimization you are closer to the real program you want to probe... but it will be sometimes just impossible to recognize the source code and unrolled functions that the profiler points to.