Aussie AI
The prof utility
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
The prof utility
Under Linux, and other variants of UNIX, the standard C profiling utility is called “prof
”. This utility calculates the
percentage time taken by each function. This is valuable information when considering
which functions to make more efficient.
To use prof
, compile the program with the −p
option to the compiler (strictly speaking, the
-p
option is needed only at the link stage of compilation) and then execute the program.
Provided the program terminates normally or via exit, a data file called “mon.out
”
will be generated. This file contains the data to be used by prof in preparing an
execution profile for the program. To examine this profile, type the command:
prof
If your executable is not called a.out
, but say, my_prog
, the command is:
prof ./my_prog
This command will generate a profile of your program’s execution from which the functions that use the most time can be identified. A sample of part of the output generated by prof is:
%time seconds cum % cum sec procedure (file) 42.1 4.4700 42.1 4.47 strcmp (../strcmp.s) 40.6 4.3100 82.7 8.78 CheckWord (spell1.c) 5.9 0.6300 88.6 9.41 fgets (../fgets.c) 4.3 0.4600 92.9 9.87 initialize (spell1.c) 3.0 0.3200 96.0 10.19 tolower (../conv.c) 1.5 0.1600 97.5 10.35 read (../read.s) 1.0 0.1100 98.5 10.46 malloc (../malloc.c) 0.8 0.0800 99.2 10.54 strlen (../strlen.c) 0.5 0.0500 99.7 10.59 morecore (../malloc.c) 0.1 0.0100 99.8 10.60 open (../open.s) 0.1 0.0100 99.9 10.61 sbrk (../sbrk.s) 0.1 0.0100 100.0 10.62 fstat (../fstat.s)
Note that the percentages calculated are only approximate because the profiler uses sampling techniques during interrupts and these samples might not provide a fully accurate picture. For example, if the program has a very small and fast function, this function might be completely missed.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |