Re: Ada bench : count words

From: Dmitry A. Kazakov (mailbox_at_dmitry-kazakov.de)
Date: 03/22/05


Date: Tue, 22 Mar 2005 23:27:25 +0100

On Tue, 22 Mar 2005 01:16:09 +0000, Marius Amado Alves wrote:

Here is a FSM/OS_Lib variant, a shameless one, I must admit. Anyway it
narrowly beats:

http://dada.perl.it/shootout/wc.gcc.html

on my FC3 machine. But of course it has no chance against:

http://shootout.alioth.debian.org/great/benchmark.php?test=wc&lang=gcc&id=0&sort=fullcpu,

which uses even dirtier tricks than me! (:-))

----------------------------------------------------
with Ada.Characters.Latin_1; use Ada.Characters.Latin_1;
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.OS_Lib; use GNAT.OS_Lib;

procedure Ada_WC is

   subtype Buffer_Index is Integer range 1..4099;
   type File_Buffer is array (Buffer_Index) of aliased Character;

   Buffer : File_Buffer;
   Index : Buffer_Index := Buffer_Index'First;
   Last : Integer := Buffer_Index'First;

   Lines : Natural := 0;
   Words : Natural := 0;
   Total : Natural := 0;

begin
<<Blank>>
   if Index < Last then -- This "if" should be a subprogram,
      Index := Index + 1; -- but I don't trust GNAT!
   else
      Last := Read (Standin, Buffer (1)'Address, 4098); -- 4K! to read
      if Last = 0 then
         goto Done;
      end if;
      Total := Total + Last;
      Index := Buffer_Index'First;
   end if;
   case Buffer (Index) is
      when ' ' | HT => goto Blank;
      when LF => Lines := Lines + 1; goto Blank;
      when others => Words := Words + 1;
   end case;
    
<<Word>>
   if Index < Last then
      Index := Index + 1;
   else
      Last := Read (Standin, Buffer (1)'Address, 4098);
      if Last = 0 then
         goto Done;
      end if;
      Total := Total + Last;
      Index := Buffer_Index'First;
   end if;
   case Buffer (Index) is
      when ' ' | HT => goto Blank;
      when LF => Lines := Lines + 1; goto Blank;
      when others => goto Word;
   end case;

<<Done>>
   Put_Line
   ( Natural'Image (Lines)
   & Natural'Image (Words)
   & Natural'Image (Total)
   );
end Ada_WC;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


Relevant Pages