Search code examples
cperformanceprofilinggprof

How to profiling sections of code?


I need to profile a piece of software written in C. Now the problem is that while gprof or my own begin timer/end timer function calls would provide me time spent in each function, I would have no information about which is the most time consuming part within each function. Some may term it as micro-optimization but that is what the need of the hour is!

One of achieving this to "manually" place begin/end timer calls across for loops (there could be more than one of these). In this case, a smarter thing would be to allow enabling/disabling these calls using macros.

But I want to automate this instrumentation?

Can you tell me if a good tool exists to achieve the same? It would be ideal if I could invoke the instrumented program repeatedly from a script and then find the average of time spent in each "section" of the code. For now section is a loosely defined term but that "tool" could have a more specific definition of what a section is.

It would also be helpful if I could somehow learn which tools would be


Solution

  • You might try using Callgrind (one of Valgrind tools) in conjunction with KCachegrind. See also this question.