comp.lang.ada
 help / color / mirror / Atom feed
From: jerry@jvdsys.nextjk.stuyts.nl (Jerry van Dijk)
Subject: Re: ADA code execution times
Date: 1998/08/02
Date: 1998-08-02T00:00:00+00:00	[thread overview]
Message-ID: <Ex1FuG.2q@jvdsys.nextjk.stuyts.nl> (raw)
In-Reply-To: 35c354e5.140587456@news.gatech.edu

Brian Franklin (bf14@prism.gatech.edu) wrote:

: I asked this question before and received some replies which were of
: some use but no answers. So I'll ask again...

Well, actually you did get the answers.

: What code can I use in my ADA program to report the time.

: OS:    Windows 95
: Compiler:   GNAT
: Processor:   Pentium

To anser again:

In the Win32 enviroment there is, to my knowledge, no way to get
more accurate timing than about 1/10th of a second without using
a realtime kernel.

You said you needed millisecond accuracy.

As Tom already said, the QueryPerformance* functions are probably
the most accurate. However, try to run the test program below
several times, and see for yourself how accurate the results are.

-- test.adb
with Win_Timer;
with Ada.Text_IO;

procedure Test is
   Start, Stop : Long_Long_Integer;
begin
   Start := Win_Timer.Get_Current_Count;
   delay 1.234;
   Stop := Win_Timer.Get_Current_Count;
   Ada.Text_IO.Put_Line ("Start:" & Start'Img);
   Ada.Text_IO.Put_Line ("Stop: " & Stop'Img);
   Ada.Text_IO.Put_Line ("Time: " & Integer'Image (Win_Timer.Get_Time (Start, Stop)));
end Test;

-- win_timer.ads
package Win_Timer is

   function Get_Current_Count return Long_Long_Integer;
   -- get current count

   function Get_Time (T1, T2 : Long_Long_Integer) return Integer;
   -- get time difference between T1 and T2 in milliseconds

   No_Counter_Error    : exception;
   Counter_Read_Error  : exception;
   Invalid_Count_Error : exception;

private

   pragma Inline (Get_Time);
   pragma Inline (Get_Current_Count);

end Win_Timer;

-- win_timer.adb
package body Win_Timer is

   type Long_Long_Pointer is access all Long_Long_Integer;
   -- pointer to 64-bit integer

   Temp_Value : aliased Long_Long_Integer;
   -- 64-bit integer buffer

   Frequency : aliased Long_Long_Integer;
   -- counter frequency

   Frequency_Pointer : Long_Long_Pointer := Frequency'Access;
   -- pointer to counter frequency

   ---------------------
   -- Win32 interface --
   ---------------------
   function QueryPerformanceCounter (Count : in Long_Long_Pointer) return Integer;
   pragma Import (StdCall, QueryPerformanceCounter, "QueryPerformanceCounter");

   function QueryPerformanceFrequency (Frequency : in Long_Long_Pointer) return Integer;
   pragma Import (StdCall, QueryPerformanceFrequency, "QueryPerformanceFrequency");

   -----------------------
   -- get current count --
   -----------------------
   function Get_Current_Count return Long_Long_Integer is
      Value : Long_Long_Pointer := Temp_Value'Access;
   begin
      if QueryPerformanceCounter (Value) = 0 then
         raise Counter_Read_Error;
      end if;
      return Value.all;
   end Get_Current_Count;

   -----------------------------------------------------------
   -- get time difference between T1 and T2 in milliseconds --
   -----------------------------------------------------------
   function Get_Time (T1, T2 : Long_Long_Integer) return Integer is
      Dif : Long_Long_Integer := T2 - T1;
   begin
      if T1 > T2 then
         raise Invalid_Count_Error;
      end if;
      return Integer ((1_000 * Dif) / Frequency);
   end;

---------------------------
-- get counter frequency --
---------------------------
begin
   if QueryPerformanceFrequency (Frequency_Pointer) = 0 then
      raise No_Counter_Error;
   end if;
end Win_Timer;

-- 
-- Jerry van Dijk  | email: jdijk@acm.org
-- Leiden, Holland | member Team-Ada
-- Ada & Win32: http://stad.dsl.nl/~jvandyk




  parent reply	other threads:[~1998-08-02  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-08-01  0:00 ADA code execution times Brian Franklin
1998-08-01  0:00 ` Tom Moran
1998-08-02  0:00 ` Jerry van Dijk [this message]
1998-08-02  0:00 ` Steve Doiel
1998-08-03  0:00   ` dennison
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox