I have a program in which significant amount of time is spent loading and saving data. Now I want to know how much time each function is taking in terms of percentage of the total running time. However, I want to exclude the time taken by loading and saving functions from the total time considered by the profiler. Is there any way to do so using gprof or any other popular profiler?
-
2That's what `gprof` is actually good at (provided you have no recursion), because it doesn't sample during I/O. – Mike Dunlavey Jul 08 '11 at 02:10
4 Answers
Similarly you can use
valgrind --tool=callgrind --collect-atstart=no --toggle-collect=<function>
Other options to look at:
--instr-atstart # to avoid runtime overhead while not profiling
To get instructionlevel stats:
--collect-jumps=yes
--dump-instr=yes
Alternatively you can 'remote control' it on the fly: callgrind_control
or annotate your source code (IIRC also with branch predictions stats): callgrind_annotate
.
The excellent tool kcachegrind
is a marvellous visualization/navigation tool. I can hardly recommend it enough:

- 374,641
- 47
- 450
- 633
-
1+1 Would give more if I could. `valgrind` and `kcachegrind` are an amazing pair! – Michael Mior Jul 07 '11 at 21:54
-
I appreciate your answer, but I'm in a hurry at the moment. Don't want to learn new profilers at the moment. – MetallicPriest Jul 08 '11 at 10:27
I would consider using something more modern than gprof
, such as OProfile
. When generating a report using opreport
you can use the --exclude-symbols
option to exclude functions you are not interested in.
See the OProfile webpage for more details; however for a quick start guide see the OProfile docs page.

- 9,540
- 3
- 39
- 58
-
But OProfile is a system wide profiler. I don't think it gives very accurate results for individual process profiling. – MetallicPriest Jul 07 '11 at 21:57
-
Yes, OProfile has the ability to do system-wide profiling; but you can filter it to give data for a single application binary or process (it can tag all data it collects with the process it came from). – DaveR Jul 07 '11 at 22:02
-
-
I'm in a hurry at the moment so it would be good if some simple solution in gprof exists. – MetallicPriest Jul 08 '11 at 10:28
for a simple, basic solution, you might want log data to a csv file.
e.g. Format [functionKey,timeStamp\n]
... then load that up in Excel. Get the deltas, and then include or exclude based on if functions. Nothing fancy. On the upside, you could get some visualisations fairly cheaply.

- 8,849
- 9
- 51
- 91