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.
cc -o myprog -p main.c subr1.c subr2.cAnd the following should also work with most makefiles ..
rm *.oThe Makefile is normally set up so that every compilation command contains the Makefile variable $(CFLAGS). A typical Makefile contains a line like
make CFLAGS=-p
CFLAGS = -gnear the top. You can override that setting by supplying your own CFLAGS value on the command line as shown. If you want to supply two flags, you would enter your commands like this:
rm *.oNote that the rm *.o command should be run before make so that you force make into re-compiling all your C files with the -p flag in effect.
make 'CFLAGS=-p -g'
myprog < largeSampleData > /dev/nullNote that sending program output to /dev/null is the normal Unix way of discarding that output without seeing it.
prof myprog(I.e., the prof command followed by the name of the program.)
The gprof profiler has very similar usage instructions.
cc -o myprog -pg main.c subr1.c subr2.cor
rm *.o make CFLAGS=-pg
myprog < largeSampleData > /dev/null
gprof myprogSeveral pages of output will be produced, including three pages of explanation on how to read the tables. You may wish to save the results in a file:
gprof myprog > gprof-outputso that you can browse the explanations and tables with the more program or some other browser.
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.
cc -o myprog -a main.c subr1.c subr2.cor
rm *.o; make CFLAGS=-a
myprog < largeSampleData1 > /dev/nullWhen 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.
myprog < largeSampleData2 > /dev/null
myprog < largeSampleData3 > /dev/null
tcov main.c subr1.c subr2.cThis creates a new file from each source code file.