PROFILING

There are two standard profiling tools on Unix -- prof and gprof. Both tools show you how much time is spent in each function in your program. Prof gives the basic information, gprof gives much more. A third tool, tcov, generates similar data to prof but with a much finer level of detail; tcov is intended for test coverage applications.

The 90-10 rule is said to apply to most programs. That is, 90% of the execution time is spent in just 10% of the code. (This 10% is usually composed of inner-loops and recursive functions.) The prof and gprof tools will help you identify the important 10% so that, if performance of your program is an issue, you can concentrate your attention where it matters.
Prof and gprof sometimes help identify bugs -- if a lot of execution time is being spent in part of the program where you would not expect it, there may be an error such as a loop exit condition that is not quite right.

 

PROFILING WITH PROF

Click here for an example of the output produced by prof.

 

PROFILING WITH GPROF

The gprof profiler has very similar usage instructions.

Click here for an example of the output produced by gprof.

 

TEST COVERAGE WITH TCOV

The prof and gprof profilers provide function-level information about your program. In particular, they will tell you how many times each function was invoked.

For testing and debugging purposes, statement-level information is highly desirable. You would like to be sure that every statement in the program has been executed at least once.

The tcov program is used for statement-level coverage.

  1. You must compile your C files with the -a flag in effect. For example:
              cc -o myprog -a main.c subr1.c subr2.c 
    or
              rm *.o; make CFLAGS=-a
  2. You must run the program on a sufficiently large amount of input data. Unlike with prof & gprof, you may run the program several times, and the information will be automatically accumulated over all the runs. E.g.
              myprog < largeSampleData1 > /dev/null 
    myprog < largeSampleData2 > /dev/null
    myprog < largeSampleData3 > /dev/null
    When the program runs, it generates data files. There is one data file for each .c file in the program. For example, if one of the source code files is named subr1.c, then a new file named subr1.c.d is automatically created. Each .d file contains binary data -- it is not human readable.

  3. You should run the tcov program. The source code files should be supplied as arguments on the command. For example:
              tcov main.c subr1.c subr2.c	
    This creates a new file from each source code file.
    For example, a file named main.c.tcov is created for the main.c file. Each .tcov file is an annotated listing of the original source code that shows how many times each basic block has been executed. (A basic block is equivalent to a box on a flow chart of the program.) For the most part, this is equivalent to showing how many times each statement was executed.
Click here for an example of the output produced by tcov.

Bentley's Prime Number Program Example

Jon Bentley illustrated the use of prof and tcov on a series of programs that compute prime numbers. Click here to follow through the examples.