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.





