Print

Print


From: Bob Leif
To: Jeff Carter et al.

Precise timing measurements statements should avoid the use of
Ada.Text_Io.Put statements, such as those below, inside of a loop.

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

There is no absolute guarantee that the Put statement will not result in a
disk write rather than transfer to a cache. The data can be stored in an
array which can be sent to a file after all of the data has been acquired.

-----Original Message-----
From: Team Ada: Ada Advocacy Issues (83 & 95)
[mailto:[log in to unmask]]On Behalf Of Jeff Carter
Sent: Saturday, November 04, 2000 8:18 PM
To: [log in to unmask]
Subject: Re: Timing for MIDI: an interesting project


Without addressing the ways that Windows can be made to provide
microsecond accuracy, which I have seen discussed, perhaps here or on
comp.lang.ada, I must say that I have been unable to get the program you
posted to output numbers within 20% of the numbers you posted. I also
have no idea why you have the 30-second delay at the end of the program,
but it would make the execution time of the program exceed 90 seconds.

I have run the following modification of your program:

with Ada.Real_Time; use Ada.Real_Time;
with Ada.Text_Io;

procedure Test_Time is
   -- ----------------------------------------------------------
   --  GET THE STARTING TIME, LOOP, GET THE END TIME, SUBSTRACT
   -- ----------------------------------------------------------

   Time_Start      : Ada.Real_Time.Time;
   Time_End        : Ada.Real_Time.Time;
   Time_Difference : Ada.Real_Time.Time_Span;
   Time_Duration   : Duration;
begin -- Test_Time
   for J in 1 .. 10 loop
      Time_Start := Ada.Real_Time.Clock;

      for I in 1 .. 1 loop
         delay 60.00;
      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 loop;
end Test_Time;

on a Win98 machine and obtained the following output:

Time Difference:  60.002681071
Time Difference:  60.000495315
Time Difference:  60.000772725
Time Difference:  60.000887544
Time Difference:  60.000876649
Time Difference:  60.000895087
Time Difference:  60.000931963
Time Difference:  60.000733335
Time Difference:  60.000900115
Time Difference:  60.000691430

Thus we see that the overhead is typically less than 1 millisecond.

Processor: AMD K6-2 3D-NOW 400 Mhz
OS:        Windows 98
Compiler:  GNAT 3.13p
Options:   -gnatwu -gnato -O2 -gnatn

As expected, the program takes about 10 minutes to run.

In any case, you should be using the delay until statement to obtain
accurately repeated intervals.

Altering the program to

with Ada.Real_Time; use Ada.Real_Time;
with Ada.Text_Io;

procedure Test_Time is
   -- ----------------------------------------------------------
   --  GET THE STARTING TIME, LOOP, GET THE END TIME, SUBSTRACT
   -- ----------------------------------------------------------

   Time_Start      : Ada.Real_Time.Time;
   Time_End        : Ada.Real_Time.Time;
   Time_Difference : Ada.Real_Time.Time_Span;
   Time_Duration   : Duration;
   Next            : Time;

   Interval : constant Ada.Real_Time.Time_Span :=
      Ada.Real_Time.To_Time_Span
         (0.00344827586206896551724137931034483);
begin -- Test_Time
   for J in 1 .. 10 loop
      Time_Start := Ada.Real_Time.Clock;
      Next := Time_Start + Interval;

      for I in 1 .. 17_400 loop
         delay until Next;

         Next := Next + Interval;
      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 loop;
end Test_Time;

outputs

Time Difference:  60.002944234
Time Difference:  60.004521531
Time Difference:  59.999996647
Time Difference:  60.000005029
Time Difference:  60.000037714
Time Difference:  60.000008381
Time Difference:  60.000009219
Time Difference:  60.000003353
Time Difference:  60.000001677
Time Difference:  60.004129301

which may be acceptable for your purposes.

--
Jeff Carter
"You tiny-brained wipers of other people's bottoms!"
Monty Python & the Holy Grail