Mon, 6 Nov 2000 10:14:13 -0800
|
David's modified version conceals an important difference, beyond just the
style touchups and "letting the compiler compute the number of seconds per
tick". You might wonder: why did he write the body of the loop as:
delay until Next_Time;
Next_Time := Next_Time + Seconds_Per_Tick;
instead of just saying:
delay Seconds_Per_Tick;
???
To make a long story short... there's a clock ("time base") associated with
type Ada.Real_Time.Time. There is no requirement that the clock used for
relative delays (e.g. "delay Seconds_Per_Tick") be the same as any other
clock, in particular the clock used for Ada.Real_Time.Time. The clock used
for relative delays is implementation-defined, and is often the same clock
as used by package Calendar. The intent is that Ada.Real_Time can provide a
more accurate clock if the Calendar clock is crappy.
In your (Jesse's) original version, you were using "whatever clock" by
writing a relative delay statement. This is probably a crappy clock, and
you were timing that clock using the better clock of Ada.Real_Time.
Another possible improvement to your program would be to define
Ticks_Per_Minute : constant := Beats_Per_Minute * Ticks_Per_Beat
-- Ticks_Per_Beat is "pulses-per-quarternote" (PPQ)
Mark Lundquist
Rational Software
> -----Original Message-----
> From: Team Ada: Ada Advocacy Issues (83 & 95)
> [mailto:[log in to unmask]]On Behalf Of David C. Hoos, Sr.
> Sent: Sunday, November 05, 2000 3:13 AM
> To: [log in to unmask]
> Subject: Re: Timing for MIDI: an interesting project
>
>
> 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
>
|
|
|