TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
"David C. Hoos, Sr." <[log in to unmask]>
Reply To:
David C. Hoos, Sr.
Date:
Sun, 5 Nov 2000 05:12:31 -0600
Content-Type:
text/plain
Parts/Attachments:
text/plain (66 lines)
Here is a suggested approach, along with some suggestions
for improving program readability, as well as letting the
compiler compute the number of seconds per tick.

Note also, that I suggest not using the clause
"use Ada.Real_Time;" -- instead using the clause
"use type Ada.Real_Time.Time;" to gain visibility of the
needed operation(s) on taht type.

Finally, the package Standard rarely, (if ever) needs to
be explicitly specified as a prefix.

with Ada.Real_Time;
with Ada.Text_Io;

procedure TestTime is

   -- ----------------------------------------------------------
   --  GET THE STARTING TIME, LOOP, GET THE END TIME, SUBTRACT
   -- ----------------------------------------------------------

   Time_Start         : Ada.Real_Time.Time;
   Time_End           : Ada.Real_Time.Time;
   Time_Difference    : Ada.Real_Time.Time_Span;
   Time_Duration      : Duration;
   Next_Time          : Ada.Real_Time.Time;
   -- Program constants should be defined with names meaningful to
   -- The problem domain
   Ticks_Per_Minute   : constant := 17_400;
   Seconds_Per_Minute : constant := 60.0;
   -- Constants that are derived should be generated as compile-time
   -- computations, in terms of the values from which they're derived.
   Seconds_Per_Tick   : constant Ada.Real_Time.Time_Span :=
     Ada.Real_Time.To_Time_Span (Seconds_Per_Minute / Ticks_Per_Minute);
   use type Ada.Real_Time.Time;
begin

   Time_Start := Ada.Real_Time.Clock;
   Next_Time  := Time_Start + Seconds_Per_Tick;
   for I in 1 .. Ticks_per_minute loop
      delay until Next_Time;
      Next_Time := Next_Time + Seconds_Per_Tick;
   end loop;
   Time_End := Ada.Real_Time.Clock;
   Time_Difference := Time_End - Time_Start;

   Time_Duration := Ada.Real_Time.To_Duration(Time_Difference);

   Ada.Text_Io.Put ("Time Difference: ");
   Ada.Text_Io.Put_Line (Duration'Image (Time_Duration));

end TestTime;

Ten iterations of the program produced:

Time Difference:  60.007150628
Time Difference:  60.003654095
Time Difference:  60.000104762
Time Difference:  60.005300113
Time Difference:  60.009341408
Time Difference:  60.004193828
Time Difference:  60.007467428
Time Difference:  60.052500792
Time Difference:  60.002551161
Time Difference:  60.000667962

ATOM RSS1 RSS2