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

  • Re: String Matching Problem
    ... thrown in the buffer, so later it can be found by that other thread. ... looking for(what if the response was actually already in the buffer from ... it returns it(like the perfect scenario earlier). ...
    (comp.lang.ruby)
  • Re: how to read two bytes from string and convert to long?
    ... >> If you have set the buffer up as a String then there is a small chance ... >calling a function twice; once to get the sizw required for the data, ...
    (microsoft.public.vb.winapi)
  • Re: Cano 300d (rebel) to 350d (XT) worth the swap?
    ... >I have been using a Canon 300d for the last 10 months and don't have any ... However I now have the chance to change it to a 350d for ... XT's faster operation and larger buffer. ... 8MP over 6MP advantage is slight. ...
    (rec.photo.digital)
  • Re: how to read two bytes from string and convert to long?
    ... > If you have set the buffer up as a String then there is a small chance ... > that under some regional settings the ANSI -> UNICODE conversion will ...
    (microsoft.public.vb.winapi)
  • Re: Decimal representation of binary file (unpack)
    ... Hi John/Philip, ... Thank you both for your comments -- I have to admit that this wasn't something that I thought about but it should have been. ... print unpack 'I*', $buffer; ...
    (perl.beginners)