.TI F77/TIMING "Sep. 15, 1984"
Timing Programs, Profiles

UNIX divides the processor time charged to a job into user time and
system time.  User time is the time the processor spends computing
for the process; system time is the time the processor spends 
executing system calls (I/O requests, forks, execs, etc.)
on your behalf.

Both kinds of time can vary according to the system load
because processor time spent handling interrupts is charged
to the current user even if the activity was caused by some
other user or some system function.
In practise the user time varies much less than the system time.
The times are kept in 60th of a second units.
You are charged for both user time and system time.

Program segments may be timed using the dtime() library subroutine
(see "man 3f dtime"):

.nf
	      dimension tarray(2)
		 ...
	      call dtime(tarray)
		... program segment to be timed ...
	      call dtime(tarray)
	      print 8000, tarray
	8000  format(" user time = ",f8.3," system time = ",f8.3)
.fi

Dtime() in this example will put the user time used by the 
program segment in tarray(1) and the system time used in tarray(2).

It is best to exclude I/O operations from the segment being timed.
Since the times are kept in units of a 60th of a second and vary by
system load, do not try to time any computation that takes less than 10
or 20 seconds.  If the computation is fast, put it in a loop and
time 10 or 100 repetitions.

It is also possible to profile an entire program and find out
how often each subprogram was called and how much time it used.
First compile the program with the '-p' flag, run it and then
execute the prof utility to print out the profile:

.nf
	% f77 -p prog.f
	prog.f:
	   MAIN:
	   sub1:
	   sub2:
	   sub3:
	Loading a.out ...
	% 
	% a.out
	    ... program output ...
	% prof
	
		%time	cumsecs	#call	ms/call	name
		 41.1	 3.37	  2	1683.48	_sub1_
		 34.2	 6.17	  1	2800.24	_sub2_
		 12.2	 7.17	  1	1000.09	_MAIN__
		 11.8	 8.13	  2	 483.37	_sub3_
		  0.4	 8.17			_fstat
		  0.2	 8.18			_x_putc
.fi

In this example, sub1 was called twice and used 3.37 seconds of processor
time (41% of the total), sub2 was called once and used 
6.17 - 3.37 = 2.80 seconds (34%), etc.  Prof keeps track of the sum
of user and system times.  The last two lines of the output are for 
library routines which were not compiled with the profile option.

For more extensive profiling statistics, use the '-pg' option and the
gprof command.  See "man f77" and "man gprof" for details.

It is also possible to time entire commands.  You can do it directly
by invoking the C Shell's builtin time command:

.nf
	% time a.out
	6.5u 4.6s 0:23 48% 12+47k 690+688io 17pf+0w
.fi

This is partially documented in "man csh" and more fully in "An
Introduction to the C shell", section 2.8.  It tells us the command
used 6.5 seconds of user time, 4.6 seconds of system time and took 23
seconds of real (clock) time to finish.  During those 23 seconds, it
used 48% of the cpu cycles( 48% = (6.5+4.6)/23. ).  On the average, it
used 12k bytes of program space and 47k bytes of data space.  The
program did 690 disk reads and 688 disk writes, had 17 page faults and
did not have any pages swapped out.

A second method for timing a program is to use /bin/time:

.nf
	% /bin/time  a.out
	       46.0 real         5.5 user         3.8 sys  
.fi

It is similar to the C shell time, but gives much less information.
Note that both commands are named time.  If you type "man time",
you will be told about /bin/time rather than the command built
into the C shell.

A third way is to set the C shell variable "time" to a minimum.
Thereafter, if a command takes more than that amount of cpu time
(system plus user time),
the statistics will automatically be listed using the same format as
the C shell time command.  This is reversed by the unset time command.

.nf
	% set time = 4
	% a.out
	5.7u 4.3s 0:59 16% 12+47k 703+654io 4pf+0w
	% 
	% unset time
	% a.out
	%
