Rohit Banga
Rohit Banga

Reputation: 18926

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

Upvotes: 2

Views: 548

Answers (3)

Mike Dunlavey
Mike Dunlavey

Reputation: 40699

You want the code to run as fast as possible, right?

gprof is a measuring tool. It can help in evaluating alternative implementations, as the original authors wrote. They did not say it is effective for locating the code needing an alternative implementation, and it isn't, even though nearly everyone thinks it is.

The fallacy is that measuring locates, but if you want to find an elephant in the room, do you need to measure it to know it's there? No, you open your eyes.

Here's a way to open your eyes to what your program is doing.

Upvotes: -1

THV
THV

Reputation: 77

I have not used it myself, but I have heard that the Valgrind instrumentation framework (http://www.valgrind.org/) has tools that enable the very fine-grained profiling necessary for what you are trying to accomplish.

Upvotes: 0

Vojislav Stojkovic
Vojislav Stojkovic

Reputation: 8153

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

Upvotes: 1

Related Questions