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!news1.google.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: Ada bench : count words Date: Tue, 22 Mar 2005 01:16:09 +0000 Organization: Cuivre, Argent, Or Message-ID: References: <87vf7n5njs.fsf@code-hal.de> <423f5813$0$9224$9b4e6d93@newsread4.arcor-online.net> 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 1111454206 50016 212.85.156.195 (22 Mar 2005 01:16:46 GMT) X-Complaints-To: usenet@melchior.cuivre.fr.eu.org NNTP-Posting-Date: Tue, 22 Mar 2005 01:16:46 +0000 (UTC) To: comp.lang.ada@ada-france.org Return-Path: In-Reply-To: <423f5813$0$9224$9b4e6d93@newsread4.arcor-online.net> X-Mailer: Apple Mail (2.619.2) X-OriginalArrivalTime: 22 Mar 2005 01:16:13.0336 (UTC) FILETIME=[BD1A3180:01C52E7C] 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:9707 Date: 2005-03-22T01:16:09+00:00 I took a shot at the count-words benchmark, a program to count lines, words and characters. The Ada program currently published there is broken. My program is correct and portable but: - the speed is circa 1/3 of the GCC C version - it fails to comply with the requirement that the input be taken from standard input. To implement buffering, I have resorted to Ada.Direct_IO, which I think cannot apply to standard input. Can you help with any of these points? Thanks. The complete program follows. -- Count words in Ada for the language shootout -- by Marius Amado Alves with Ada.Characters.Handling; with Ada.Characters.Latin_1; with Ada.Command_Line; with Ada.Direct_IO; with Ada.Strings.Fixed; with Ada.Text_IO; procedure Count_Words is use Ada.Characters.Handling; use Ada.Characters.Latin_1; use Ada.Command_Line; Filename : String := Argument (1); Buffer_Size : constant := 4096; EOF : Character := FS; EOL : String := (1 => LF); Lines : Natural := 0; Words : Natural := 0; Total : Natural := 0; In_Word : Boolean := False; function Is_Separator (C : Character) return Boolean is begin return Is_Control (C) or C = ' '; end; procedure Start_Word is begin In_Word := True; end; procedure Finish_Word is begin Words := Words + 1; In_Word := False; end; procedure Process (S : in String) is begin Lines := Lines + Ada.Strings.Fixed.Count (S, EOL); for I in S'Range loop if Is_Separator (S (I)) then if In_Word then Finish_Word; end if; else if not In_Word then Start_Word; end if; end if; end loop; end; begin declare package Character_IO is new Ada.Direct_IO (Character); use Character_IO; File : File_Type; begin Open (File, In_File, Filename); Total := Natural (Size (File)); Close (File); end; declare subtype Buffer_Type is String (1 .. Buffer_Size); package Buffer_IO is new Ada.Direct_IO (Buffer_Type); use Buffer_IO; File : File_Type; S : Buffer_Type; begin Open (File, In_File, Filename); for I in 1 .. Total / Buffer_Size loop Read (File, S); Process (S); end loop; Close (File); end; declare subtype Rest_Type is String (1 .. Total rem Buffer_Size); package Character_IO is new Ada.Direct_IO (Character); use Character_IO; File : File_Type; S : Rest_Type; begin Open (File, In_File, Filename); Set_Index (File, Count (Total - S'Length)); for I in 1 .. S'Length loop Read (File, S (I)); end loop; Close (File); Process (S); end; if In_Word then Finish_Word; end if; Ada.Text_IO.Put_Line (Natural'Image (Lines) & Natural'Image (Words) & Natural'Image (Total)); end;