How exactly does gprof work?

Alex picture Alex · Feb 12, 2011 · Viewed 8.2k times · Source

This is something of a bloated question, so I apologize ahead of time. I'm curious about how gprof works on a low technical level. I understand it's done by timers, but then why does the executable need to be specially compiled to be profiled? Does the compilation cause space to be allocated for statistics?

Also, how is the timing done exactly?

Answer

Dr G picture Dr G · Feb 13, 2011

Well, this gives a good explanation. Also this explains statistical profiling

Essentially gprof will change the executable of your program (this is called instrumenting the code) to store some book-keeping information, e.g. how many times a function is called.

The statistical profiling bit comes from snooping the program counter regularly to get a sample of what your code is doing.

Gprof does both. It instruments the code and collects samples from looking at the program counter.