
How to use gprof
Using the gprof tool is not at all complex. You just need to do the following on a high-level:
- Have profiling enabled while compiling the code.
- Execute the program code to produce the profiling data.
- Run the gprof tool on the profiling data file (generated in the step above).
Full Answer
How do I use the gprof tool?
Using the gprof tool is not at all complex. You just need to do the following on a high-level: Run the gprof tool on the profiling data file (generated in the step above). The last step above produces an analysis file which is in human readable form.
How do I use gprof exec-O2?
Type gprof exec > out where exec is replaced by your executable's name and out by some meaningful name for the profiling information. For instance, if your executable were "foo" on the third run compiled with the -O2 option then you might type: Look over the output and learn what it means.
What is gprof in gmon?
Gprof reads the given object file (the default is a.out) and establishes the relation between its symbol table and the call graph profile from gmon.out . If more than one profile file is specified, the gprof output shows the sum of the profile information in the given profile files.
What does the-PG option do in gprof?
The -pg option also links in versions of the library routines that are compiled for profiling. Gprof reads the given object file (the default is a.out) and establishes the relation between its symbol table and the call graph profile from gmon.out .

How gprof works?
Gprof works by automatically instrumenting your code during compilation, and then sampling the application's program counter during execution. Sampling data is saved in a file, typically named gmon. out, which can then be read by the gprof command.
How do I enable gprof?
EXAMPLESEnable profiling during compilation (use -pg option) $ gcc -pg -o TestGprof TestGprof.c.Execute the binary so that profiling data is generated. $ ./TestGprof. If the profiling is enabled then on executing the program, file gmon. ... Run gprof on profiling data. $ gprof -b TestGprof gmon.out > analysis.out.
How do I use gprof on Windows?
First of all, the user has to compile the C/C++ program with profiling enabled using the -pg option prior to running the tool. This can be done via the project Properties->C/C++ Build->Settings->Tool Settings->GCC C Compiler->Debugging tab which has a check-box "Generate Gprof Information (-pg)".
What is gprof command?
On Unix-like operating systems, the gprof command is a software developement tool that displays call graph profile data of a compiled binary.
How do I know if Gprof is installed?
To check that gprof is installed properly, execute the gprof command and it should give some error like 'a. out: No such file or directory'. Assuming that the compiler being used it gcc or cc, compile your code with the option '-pg' so that the executable includes extra code for profiling purposes.
How do I run Cachegrind?
The two steps are:Run your program with valgrind --tool=cachegrind in front of the normal command line invocation. When the program finishes, Cachegrind will print summary cache statistics. ... Generate a function-by-function summary, and possibly annotate source files, using the supplied cg_annotate program.
What is GCC option?
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker.
How accurate is Gprof?
They are completely accurate and will not vary from run to run if your program is deterministic. The sampling period that is printed at the beginning of the flat profile says how often samples are taken. The rule of thumb is that a run-time figure is accurate if it is considerably bigger than the sampling period.
What is a GMON out file?
The `gmon. out' file is written in the program's current working directory at the time it exits. This means that if your program calls chdir , the `gmon. out' file will be left in the last directory your program chdir 'd to.
DESCRIPTION
gprof produces an execution profile of C, Pascal, or Fortran77 programs. The effect of called routines is incorporated in the profile of each caller. The profile data is taken from the call graph profile file ( gmon.out default) which is created by programs that are compiled with the -pg option of cc, pc, and f77 .
OPTIONS
These options specify which of several output formats gprof should produce.
Deprecated Options
These options have been replaced with newer versions that use symspecs.
EXAMPLES
Consider below program for which you need to generate profiling data: TestGprof.c
Gprof Setup & Usage
Here are some of the steps required for downloading and setting an environment for gprof:
Flat Profile
The statistics displayed above is known as flat profile. Here is the explanation (taken from the file output) of what each column means :
Call Graph
This table describes the call tree of the program, and was sorted by the total amount of time spent in each function and its children. Each entry in this table consists of several lines. The line with the index number at the left hand margin lists the current function.
What is Gprof?
So, what exactly is Gprof? According to the tool's official documentation, it gives users an execution profile of their C, Pascal, or Fortran77 programs. What Gprof basically does is, it calculates the amount of time spent in each routine or function. "Next, these times are propagated along the edges of the call graph.
Download and Install Gprof
First check whether or not the tool is already installed on your system. To do this, just run the following command in a terminal.
Gprof Usage
Needless to say, the best way to understand a tool like Gprof is through a practical example. So, we'll start off with a C language program, which we'll be profiling through Gprof. Here's the program:
What is gprof?
Gprof is a profiling program which collects and arranges statistics on your programs.Basically, it looks into each of your functions and inserts code at the head and tail of each one to collect timing information (actually, I don't believe it checks each time the function is run, but rather collects statistically significant samples).
How to Use GProf in 5 Easy Steps
Get your program working!! gprof is not a debugger. Use it once you have a working program to optimize that program.
Some advice
Read the stuff at the beginning. It does a good job of explaining the output you get from gprof.

Compiling and Linking to Enable Profiling with gprof
Collecting Profiling Data
- To collect profiling data, simply run your gprof-enabled executable the same way you would run a non-gprof-enabled executable. The data will be collected in a file called gmon.out.
Generating ASCII gprof Output
- You can use the gprof command to convert binary data in gmon.outinto a human-readable format. There are two types of output: flat profile and call graph. The flat profile shows how much time your program spent in each function, and how many times that function was called. This profile helps to identify hotspots in your application. Hotspots are shown at the top of the flat profile. T…
Commonly Used gprof Command Options
- Some common options are described here. Read man gproffor more information. 1. -p prints a flat profile. For example: gprof -p a.out gmon.out.1001 gmon.out.1002 2. -q prints a call graph. For example: gprof -q a.out gmon.out.* 3. -s sums up the information from multiple profiling data files and produces a file called gmon.sum for analysis. For exam...