Before you start doing any optimization at all, check which parts of your code are slowing the machine down -- there's no point in tweaking, for example, a one-time initialisation routine which takes only a tiny fraction of the program's runtime. You do this by using a profiler.
Different compilers will invoke a profiler (presuming
they have one) in different ways. The Sun and Digital
Fortran compilers include profiling code if you give the
option -pg
to the f77
command.
Compile and link the program with this option (if
you wish, you can compile only those modules you want to
profile with the -pg
option, but you must
include the option in the final link command), and then
run it. This run will create a file gmon.out
in your current directory. Then you can run
gprof
. Taking as example the programs in
Appendix A.4, we can build and profile them as
follows:
% f77 -pg -o timewaster p1.f p2.f p3.f p1.f: MAIN silly: p2.f: mkidentity: p3.f: determinant: Linking: % timewaster 1.00000 % ls gmon.out p1.o p2.o p3.o p1.f p2.f p3.f timewaster* % gprof timewaster >timewaster.gprof
The output file timewaster.gprof
is many lines long, even
though the program is so short! The file contains a great deal of
information, but buried amongst it is the following display (from the
Sun profiler, trimmed):
This indicates that the subroutinecalled/total parents index %time self descendents called+self name index called/total children 0.05 0.64 1/1 main [2] [1] 97.2 0.05 0.64 1 MAIN_ [1] 0.64 0.00 100000/100000 mkidentity_ [4] 0.00 0.00 1/1 __s_wsle_nv [167] 0.00 0.00 1/1 determinant_ [8] 0.00 0.00 1/1 __do_l_out [157] 0.00 0.00 1/1 __e_wsle [158] ----------------------------------------------- 0.64 0.00 100000/100000 MAIN_ [1] 4] 90.1 0.64 0.00 100000 mkidentity_ [4]
mkidentity
was called 100000 times from the main program, and that
90.1% of the time -- a total of 0.64 seconds -- was spent
in that routine. Were this a real project, this would
indicate that this routine would be a good one to examine
for speedups.This example, and a further one using the
tcov
utility, were taken from
[sunf77], which might be consulted for
further details. pixie
is the corresponding
utility on Compaqs -- see its man-page for details [RW].
gprof
is not specific to Sun, but is
available for other architectures, and other compilers
(including gcc
) as well.