How to Use gprof (The Basics)

(0 votes, average 0 out of 5)

gprof is a GNU profiler, which allows users to see important information about their code such as how much time is spent in a function or how often certain call trees are executed.  It's a very powerful program -- this article will give you only the information you need to know to see a basic report.  Here's how to do it.

 

1.  Compile Your Code with the -pg flag

Compile your C code as you typically would, but add in a -pg flag.  This indicates to the compiler that it should include some extra binary for profiling.

gcc -pg -o example example.c

 

2.  Run the Program

Once you compile, run the program as you typically would, with typical command-line arguments as if you were running the program in the "real world".  You should now see a file in the same directory called gmon.out.  This file is used by gprof to build your profile report.

sh> ./example
sh> ls
 
example.c  example  gmon.out


3.  Run gprof

Now you are ready to use gprof!  Run gprof using the following syntax:

sh> gprof  [ > output_file]

You most likely want to use the redirect to the output file, as the report is lengthy and is useful to view in a text editor.  Using our example above, we could gprof like so:

sh> gprof example > output.txt

This will create an output file called output.txt which contains the report. Open the report in your favorite text editor to see the results.  It will tell you the amount of time spent in each function, how many times each function it is called, etc.  Most of it is fairly self-explanatory and will not be explained in any detail here.  

 

Now you know how to use gprof!  It's a very powerful program but using it on the most basic level is its most common use and is quick and easy to do.  

Partner Links:
Last Updated on Monday, 04 October 2010 14:49  
Related Articles

» How to Keep a Window Always on Top in Windows

Each iteration of Windows has welcomed some great functionality, but it still lacks an ability which can be very useful: keeping a window permanently visible.  Having a video or reference document visible can be great while trying to multi-task in other windows. Thankfully, third-party solutions exist to work around this problem.All that is needed is a quick, simple download.  Download from box.net, and unzip it somewhere easily accessible (such as your desktop).  Run the...

» Resolve Input/Output error when trying to write to WebDAV (davfs2) mount like box.net on Linux

These instructions assume that you already configured the WebDAV filesystem by following .If you are mounting a WebDAV folder on Linux using the davfs2 FUSE filesystem, you may encounter the following error when trying to write a file to the WebDAV filesystem:cp: cannot create regular file `test.txt': Input/output errorThis is caused by the WebDAV filesystem not supporting file locks. The solution is to disable file locks in the davfs2 configuration file, located...

» How to Change Mouse Sensitivity in Windows 7

To change how sensitive your mouse is in Windows 7, navigate to the Start button in the left hand corner and click "Control Panel".  In the search box, search for "mouse".Click on the "Change mouse settings" option and navigate to the "pointer options" tab on the top.The "Select a pointer speed" bar controls how sensitive your mouse is.  Adjust it accordingly and press Apply to save the changes.