[comp.lang.ada] Unix/Ada CPU timer

mfeldman@seas.gwu.edu (Michael Feldman) (04/19/91)

# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
#
# Wrapped by Michael Feldman <mfeldman@sparko> on Fri Apr 19 11:09:19 1991
#
# This archive contains:
#	unixtime.c	cpuclock.ada	testclok.ada	
#

unset LANG

echo x - unixtime.c
cat >unixtime.c <<'@EOF'

#include <sys/types.h>
#include <sys/times.h>

     /* code for including or porting to Ada
        returns time from program start to call time */

                  /* J.H.H.     (5-14-87) */

int unixtime()
{
  int t;
  struct tms bslot, *buffer;
  
  buffer = (struct tms *)calloc(1,sizeof(bslot));

  times(buffer);

  t = buffer->tms_utime + buffer->tms_stime
      + buffer->tms_cutime + buffer->tms_cstime ;
  return t;
}
@EOF

chmod 644 unixtime.c

echo x - cpuclock.ada
cat >cpuclock.ada <<'@EOF'
package CPUClock is
   type CPUSecond is digits 6;
   -- all float operations available
   procedure ResetCPUTime;
   function  CPUTime return CPUSecond;
   procedure Put(T: CPUSecond);
end CPUClock;
 
With TEXT_IO;
package body CPUClock is
   package CPUSecond_IO is new TEXT_IO.FLOAT_IO(CPUSecond);
 
   function UnixTime return integer;
   pragma interface(C, UnixTime, "unixtime");

   SAVED_TIME: INTEGER;
 
   function CPUTime return CPUSecond is
      begin
        return CPUSecond(UnixTime - Saved_Time)/60.0;
      end CPUTIME;
 
   procedure ResetCPUTime is
 
      begin
        SAVED_TIME := UnixTime;
      end ResetCPUTime;
 
   procedure Put(T: CPUSecond) is
 
      begin
         CPUSecond_IO.PUT(Item=>T,aft=>2,exp=>0);
      end Put;
 
begin -- initialization of package
    ResetCPUTime;
end CPUClock;
@EOF

chmod 644 cpuclock.ada

echo x - testclok.ada
cat >testclok.ada <<'@EOF'
with TEXT_IO, CPUClock;
use  TEXT_IO, CPUClock;
procedure TestClok is
 
  SavedTime, T: CPUSecond := 0.0;
  NumberOfTrials: constant INTEGER := 10;
 
  MaxIndex: constant INTEGER := 100;
  A: array(1..MaxIndex,1..MaxIndex) of INTEGER;
 
begin
    ResetCPUTime;
    for count in 1..NumberOfTrials loop
       for waste in 1..20 loop
       for row in 1..MaxIndex loop
          for col in 1..MaxIndex loop
              A(row,col) := row*col;
          end loop;
       end loop;
       end loop;
       T := CPUTime;
       put("CPU Time Used was "); put(T-SavedTime); NEW_LINE;
       put("CPU Time so far   "); put(T); NEW_LINE;
       SavedTime := T;
    end loop;
end TestClok;
@EOF

chmod 644 testclok.ada

exit 0