return to first page linux journal archive
keywordscontents

Listing 1. Ada Equivalent of Java and C++ Program

package Perf_Test is<\n>
   type Perf_Test is tagged
      record
         Amount : Integer := 0;
      end record;
   type Perf_Test_Ptr is access Perf_Test'Class;

   procedure Add_Amount (This : Perf_Test_Ptr; Val : in Integer);
   function Get_Amount(This : Perf_Test_Ptr) return Integer;

end Perf_Test;


package body Perf_Test is
  procedure Add_Amount (This : Perf_Test_Ptr; Val: in Integer) is
  begin
     This.Amount := This.Amount + Val;
  end Add_Amount;

  function Get_Amount(This : Perf_Test_Ptr) return Integer is
  begin
     return This.Amount;
  end Get_Amount;

end Perf_Test;


with System.OS_Primitives;
with Ada.Text_Io;

with Perf_Test;

procedure perftest is

   NOBJECTS : Integer := 500000;

   Ptarr : array(1..NOBJECTS) of Perf_Test.Perf_Test_Ptr;

   Sum : Integer :=0;

   Start_Time : Duration;
   End_Time   : Duration;
   Time       : Duration;

begin

   Start_Time := System.OS_Primitives.Clock;

   for I in 1..NOBJECTS loop
      Ptarr(I) := new Perf_Test.Perf_Test;
   end loop;

   for I in 1..NOBJECTS loop
      Perf_Test.Add_Amount(Ptarr(I),I);
   end loop;

   for I in 1..NOBJECTS loop
      Sum := Sum + Perf_Test.Get_Amount(Ptarr(I));
   end loop;

   End_Time := System.OS_Primitives.Clock;

   Time := (End_Time - Start_Time) * 1000;

   Ada.Text_Io.Put_Line("Elapsed time in milliseconds for " &
Integer'Image(NOBJECTS) & " objects: " & duration'Image(Time));
   Ada.Text_Io.Put_Line("Objects per millisecond:" &
Integer'Image(NOBJECTS / Integer(Time)));

end perftest;