time.sa
Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
---------------------------> Sather 1.1 source file <--------------------------
class TIME
class TIME is
-- Routines associated with timing.
-- ANSI C provides very limited support for times, in particular
-- makes it difficult to get at high resolution timers in a
-- portable way. For the moment, only the ANSI routines are used,
-- but in the future more support will be nice.
-- I avoid using the ANSI clock() because it trivially rolls over
-- when measured in microseconds, as happens on suns. It also
-- measures the processor time including child processes, which is
-- somewhat ambiguous with a parallel make utility like the
-- compiler uses.
private shared time_at_start:FLTD;
seconds:FLTD is
-- Seconds since 00:00:00 UTC, January 1, 1970. Precision is
-- not guaranteed beyond seconds. Built-in.
builtin TIME_SECONDS;
end;
start is
-- Begin timing.
time_at_start:=seconds;-- TIME::time_at_start TIME::seconds
end;
elapsed:FLTD is
-- Return time since calling "start". Precision is not
-- guaranteed beyond seconds.
return seconds - time_at_start;-- TIME::seconds TIME::time_at_start
end;
end;
class TIMES
class TIMES is
-- return system, user and wall time in clock ticks
-- This routines are not portable and may return different values
-- on different systems. use at your own risc
readonly attr sys_time,
user_time,
child_sys_time,
child_user_time,
wall_time:INT;
create:SAME is r::=new;r.now;return r; end;
now is builtin TIMES_NOW; end;
clk:INT is builtin TIMES_CLK; end;
-- to measure the time used during some parts of the program,
-- use start and elapsed. Elapsed returns the time since the last
-- start.
start is now; end;
elapsed:TIMES is return #TIMES-self; end;
plus(a:SAME):SAME is
n:TIMES:=#;
n.sys_time:=sys_time+a.sys_time;
n.user_time:=user_time+a.user_time;
n.wall_time:=wall_time+a.wall_time;
n.child_sys_time:=sys_time+a.child_sys_time;
n.child_user_time:=user_time+a.child_user_time;
return n;
end;
minus(a:SAME):SAME is
n:TIMES:=#;
n.sys_time:=sys_time-a.sys_time;
n.user_time:=user_time-a.user_time;
n.wall_time:=wall_time-a.wall_time;
n.child_sys_time:=sys_time-a.child_sys_time;
n.child_user_time:=user_time-a.child_user_time;
return n;
end;
-- standard way to print the time
str:STR is
r::="";
r:=r.append("wall time: ",#FMT("<#.##>",(wall_time.flt/clk.flt)).str," sec.\n");
r:=r.append("user time: ",#FMT("<#.##>",(user_time.flt/clk.flt)).str," sec.\n");
r:=r.append("system time: ",#FMT("<#.##>",(sys_time.flt/clk.flt)).str," sec.\n");
r:=r.append("user+system time: ",#FMT("<#.##>",((user_time+sys_time).flt/clk.flt)).str," sec.\n");
r:=r.append("CPU usage: ",((user_time+sys_time).flt*100.0/wall_time.flt).int.str," %\n");
return r;
end;
end;
-- vim:sw=3:nosmartindent