From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, MAILING_LIST_MULTI autolearn=unavailable autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8de7eedad50552f1 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news.glorb.com!proxad.net!usenet-fr.net!enst.fr!melchior!cuivre.fr.eu.org!melchior.frmug.org!not-for-mail From: Marius Amado Alves Newsgroups: comp.lang.ada Subject: Re: Ada bench : count words Date: Wed, 23 Mar 2005 15:09:26 +0000 Organization: Cuivre, Argent, Or Message-ID: References: <00b362390273e6c04844dd4ff1885ee0@netcabo.pt> NNTP-Posting-Host: lovelace.ada-france.org Mime-Version: 1.0 (Apple Message framework v619.2) Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit X-Trace: melchior.cuivre.fr.eu.org 1111590595 9483 212.85.156.195 (23 Mar 2005 15:09:55 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Wed, 23 Mar 2005 15:09:55 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: <00b362390273e6c04844dd4ff1885ee0@netcabo.pt> X-Mailer: Apple Mail (2.619.2) X-Virus-Scanned: by amavisd-new-20030616-p10 (Debian) at ada-france.org X-BeenThere: comp.lang.ada@ada-france.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Gateway to the comp.lang.ada Usenet newsgroup" List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Xref: g2news1.google.com comp.lang.ada:9802 Date: 2005-03-23T15:09:26+00:00 > I'll review this tomorrow on the bus to work (I think your program is > separating words at buffer end, and it should not). Done. Tmoran, sorry, my first sight was wrong, your algorithm is mostly fine. Minor reservations: - special statute given to CR; personally I think all characters (control or not) should count to total - reliance on Stream_Element representing one character; portable across all range of environments? My own program rewritten with Text_Streams attains the same speed as yours. The fact that it is structured doesn't hit performance significantly. Pragma Inline improves a little bit. Real times (ms) on my iBook, for input file repeated 2500 times, all compiled with only -O3: C ........................ 600 Yours, or mine inlined ... 700 Mine not inlined ......... 750 So we should submit yours, or mine inlined. It will put Ada right after the Cs w.r.t. speed. W.r.t. executable file size Ada is much bigger. On my iBook: C ....... 15k Mine .... 423k Yours ... 459k W.r.t. source code size all are similar. Number of significant semicolons (Ada), or semicolons + {} blocks + #includes + #defines (C): C .............. 27 Yours .......... 33 Mine inlined ... 52 My program follows for reference. It accepts the code for EOL as an argument. Default = 10 (LF). -- The Great Computer Language Shootout -- http://shootout.alioth.debian.org/ -- -- contributed by Guys De Cla with Ada.Characters.Handling; with Ada.Characters.Latin_1; with Ada.Command_Line; with Ada.Streams; with Ada.Streams.Stream_IO; with Ada.Strings.Fixed; with Ada.Text_IO; with Ada.Text_IO.Text_Streams; procedure Count_Words_Portable is use Ada.Characters.Handling; use Ada.Characters.Latin_1; use Ada.Command_Line; use Ada.Streams; use Ada.Streams.Stream_IO; use Ada.Text_IO; use Ada.Text_IO.Text_Streams; Buffer : Stream_Element_Array (1 .. 4096); Input_Stream : Ada.Text_IO.Text_Streams.Stream_Access := Ada.Text_IO.Text_Streams.Stream (Current_Input); EOL_Character_Pos : Stream_Element := Character'Pos (LF); Lines : Natural := 0; Words : Natural := 0; Total : Natural := 0; In_Word : Boolean := False; N : Stream_Element_Offset; Is_Separator : array (Stream_Element) of Boolean := (0 .. 32 | 127 .. 159 => True, others => False); procedure Begin_Word is begin Words := Words + 1; In_Word := True; end; procedure End_Word is begin In_Word := False; end; procedure End_Line is begin Lines := Lines + 1; End_Word; end; procedure Count_Words (S : in Stream_Element_Array) is begin Total := Total + S'Length; for I in S'Range loop if S (I) = EOL_Character_Pos then End_Line; else if Is_Separator (S (I)) then if In_Word then End_Word; end if; else if not In_Word then Begin_Word; end if; end if; end if; end loop; end; pragma Inline (Begin_Word, End_Word, End_Line, Count_Words); begin begin EOL_Character_Pos := Stream_Element'Value (Argument (1)); exception when Constraint_Error => null; end; Ada.Text_IO.Put_Line ("EOL =>" & Stream_Element'Image (EOL_Character_Pos)); loop Read (Root_Stream_Type'Class (Input_Stream.all), Buffer, N); Count_Words (Buffer (1 .. N)); exit when N < Buffer'Length; end loop; Ada.Text_IO.Put_Line (Natural'Image (Lines) & Natural'Image (Words) & Natural'Image (Total)); end;