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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8de7eedad50552f1 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news1.google.com!news.maxwell.syr.edu!transit.news.xs4all.nl!border2.nntp.ams.giganews.com!nntp.giganews.com!newsfeeder.wxs.nl!feeder.enertel.nl!nntpfeed-01.ops.asmr-01.energis-idc.net!newshub3.home.nl!newshub1.home.nl!home.nl!not-for-mail From: Andre Newsgroups: comp.lang.ada Subject: Re: Ada bench : count words Date: Wed, 30 Mar 2005 18:08:55 +0200 Organization: @Home Benelux Message-ID: References: <00b362390273e6c04844dd4ff1885ee0@netcabo.pt> NNTP-Posting-Host: cp538812-a.venra1.lb.home.nl Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news2.tilbu1.nb.home.nl 1112198931 28673 84.30.194.114 (30 Mar 2005 16:08:51 GMT) X-Complaints-To: usenet@corp.home.nl NNTP-Posting-Date: Wed, 30 Mar 2005 16:08:51 +0000 (UTC) User-Agent: Mozilla Thunderbird 1.0RC1 (Windows/20041201) X-Accept-Language: en-us, en In-Reply-To: Xref: g2news1.google.com comp.lang.ada:10163 Date: 2005-03-30T18:08:55+02:00 List-Id: Marius Amado Alves wrote: >> 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; > I checked with the Shootout side. The Ada program sent in was rated with Error. So, maybe you can check why and correct it. Andr�