How to Use Gprof Command on Linux

How to Use Gprof Command on Linux

We’re here to talk about how to use the Gprof command on Linux for profiling in Linux, and here’s everything you need to know. 

The objective of profiling is to analyze your program code and see which part of the code is taking a large amount of time for execution such that the part of the code can be rewritten. This will enable the program to achieve desired execution speed. In addition, profiling can prove to be very handy in spotting codes that are potentially error-prone, and then they can be sorted out via refactoring.

Gprof is a Linux-based tool that can be used for profiling. We will discuss the gprof tool in this article and its various options. The tool produces the profile information for any program written in C, Pascal, or Fortran.

How to Use Gprof Command on Linux?

Gprof tool

Using the gprof is quite simple. First, you need to enable the profiling when you compile the code. Now, when you execute the program, profile data is generated. Finally, you can run the gprof tool on the profiling data generated during execution. This will produce an analysis file. This file contains several tables, including a flat profile and a call graph. 

Flat profile

The flat profile provides information such as the number of times a function is called, the time it takes to execute a function, etc. This profile information can be used to analyze which function is taking most of the CPU cycles.

Call graph

The call graph shows the functions that are called from another function. It also estimates the number of times this function is called. In this way, you may eliminate the function calls that take a lot of time.

Enable profiling while compiling

To enable profiling while compiling, simply add the -pg option in the compilation step. The pg flag generates extra code for profiling that is used by the gprof command. The following command illustrates how we can compile profiling information:

$ gcc -Wall -pg program.c program_new.c -o program

Where, the program is the name of the program whose profile information, you want to see.

Execute the code to generate profile information

The binary file generated above can be executed to generate profile information. The following line executes the code:


This will also generate a file gmon.out in the current working directory. To see what files are generated via execution of the above command, simply type ls as follows:


Run the gprof tool

Now, we will run the gprof tool providing as an argument the output file and gmon.out file. This will produce the profiling information:

$ gprof program gmon.out > analysis.txt

Tip: While using gprof, you can specify the output file, or alternatively, the output will be shown on the standard output stream.

gprof options

We will now discuss various options of gprof along with examples.

Suppress the printing information

In gprof, to suppress the printing information for static functions, one can use the -a flag as follows:

$ gprof -a program gmon.out > analysis.txt

This will suppress the information about the static functions of the program.

Suppress the verbose information

Gprof produces a lot of extra information that is not required. This can be suppressed with the -b flag as follows:

$ gprof -b program gmon.out > analysis.txt

This will disable the verbose information and provide only brief profiling details.

Suppressing or Printing only flat profile

If you only require a flat profile, then you can use the -p flag as follows:

$ gprof -p program gmon.out > analysis.txt

Alternatively, to suppress the flat profile, you can use the following command:

$ gprof -P program gmon.out > analysis.txt

To print information related to a specific function, you can pass the name of the function with the -p flag as follows:

$ gprof -pfun -b program gmon.out > analysis.txt

Similarly, to suppress flat profile information for specific function information, the following command can be used:

$ gprof -Pfunc program gmon.out > analysis.txt

The -q option can be used to print the call graph only as follows:

$gprof -q program gmon.out > analysis.txt

To print only specific function information in the call graph, the following can be used:

$ gprof -qfun program gmon.out > analysis.txt

Finally, the call graph can be suppressed using -Q option:

$ gprof -Q program gmon.out > analysis.txt

Other options of gprofile

There are several other options for gprofile. For instance, -A option can be used to generate annotated source code, -c to print a tally of the number of times a function is called, -Z to exclude the tally information, -i to display summary information, -v for version information and -h for help. For more information about the various options, see the manual.

Here, we discussed gprofile which is a handy tool for generating profile information for a program written in C, Pascal, or FORTRAN. A program should execute fast, producing the desired output. If it takes a lot of time, it is not optimized and has various nested function calls that require optimization. 

Otherwise, these nested calls might become bottlenecks. For this purpose, various software tools are available to generate meta information and track program code. Such programs are called profilers. We discussed gprofile in this article along with its various options.

If this guide helped you, please share it.

Leave a Reply
Previous Article
How to Use zcat Command on Linux

How to Use zcat Command on Linux

Next Article
How to Install ERPNext on Rocky Linux 9 3

How to Use xargs with Find on Linux

Related Posts