I've found gprof to be the best CPU hotspot profiler, and Google Performance Tools to be the best sampling profiler. Both work for C and C++.
In my opinion there are no good profiling tools on Windows.
GNU gprof pros and cons
- GCC only
- Works with C and C++
- Only treats CPU time, and code inside the binary, you need everything you wish to profile statically linked in
- Very accurate
- Adds a small overhead to execution
Google Performance Tools pros and cons
- I think it requires the GNU tool chain
- Occasionally fails to identify symbols
- Very customizable
- Outputs to a huge variety of formats, including the Callgrind format, and automatically loads KCacheGrind for you
- Has various memory profiling tools also
- Is a sampling profiler, with minimal overhead
Related useful questions and answers
- Alternative to -pg with Clang?
- What's your favorite profiling tool (for C++)
- Alternatives to gprof
- C++ Code Profiler
- Confusing gprof output