40

is there any extension of valgrind, that can be used in the command window, that would help me know the time, in seconds, spent in each function in my C code?

thanks =)

Syntax_Error
  • 5,964
  • 15
  • 53
  • 73
  • callgrind is valgrinds profiling tool for those purposes and outputs something like machine cycles per function, however, you could compile your program using profiling flags from gcc to create profiling output without valgrind... callgrind: http://valgrind.org/docs/manual/cl-manual.html – Christian Goltz Jul 12 '11 at 11:45
  • Ive been trying the manual since yesterday, however i havent been successful in getting the 'time in sec' spent in each function. am i missing something? – Syntax_Error Jul 12 '11 at 11:57
  • "in each function" is ambiguous two ways: 1) exclusive vs. inclusive, and 2) total vs. average per invocation. – Mike Dunlavey Jul 12 '11 at 12:08
  • @Mike: it doesn't matter as long as I get consistent data through every simulation and I get the time in sec . – Syntax_Error Jul 12 '11 at 12:11

3 Answers3

33

For machine instruction profiling use valgrind's callgrind (also, cachegrind can do cache and branch prediction profiling which is quite nice).

For time measurements use google's cpu profiler, it gives way better results than gprof. You can set sampling frequency and it can show the output as a nice annotated call graph.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
25

Valgrind isn't suited for measuring time, as running an application in valgrind distorts the results (slowdown, CPU vs. I/O). Thus valgrind profiling tool callgrind doesn't measure time but CPU instructions. Callgrind is only useful if your bottleneck is CPU-bound (thus CPU instructions matter), then CPU instructions measured will be in proportion to the time spent. It's not useful if heavy I/O or multiple processes are involved. Then you should use a sampling profiler, like sysprof or gprof (Edit 2020: perf). That checks in intervals which function the process is in, with less distorted results.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • 2
    Good info, but _gprof_ is CPU-only. _sysprof_ documentation is very uninformative, but I suspect it is also CPU-only. _gprof_ only samples the program counter (which is why it is CPU-only). It also counts the number of calls from one routine to another, and uses that to try to estimate the inclusive time in callers, but that's very problematic, especially with recursion. There's a sampling profiler that has none of these problems - [Zoom](http://www.rotateright.com/). – Mike Dunlavey Jul 12 '11 at 13:16
  • @Mike Dunlavey: It measures CPU, yes. What I meant: Using such profilers is usually less intrusive and gives more realistic measurements if CPU time is not the dominant factor. – Frank Osterfeld Jul 12 '11 at 19:41
  • Good Answer. Also want to share https://web.stanford.edu/class/cs107/resources/callgrind – Deepak Tatyaji Ahire Mar 24 '21 at 13:31
3

Use this link. I would think something like Callgrind should do the trick.

Community
  • 1
  • 1
Sriram
  • 10,298
  • 21
  • 83
  • 136
  • 3
    Ive been using callgrind, it is useful as a matter of fact, yet it is not showing the time in seconds. Kcachegrind is doing the trick but using a UI which is something Im trying to bypass! do u have any other way? – Syntax_Error Jul 12 '11 at 11:58