comp.lang.ada
 help / color / mirror / Atom feed
From: jonathan <johnscpg@googlemail.com>
Subject: Re: Ada Shootout program for K-Nucleotide (patches)
Date: Tue, 18 Aug 2009 09:59:45 -0700 (PDT)
Date: 2009-08-18T09:59:45-07:00	[thread overview]
Message-ID: <4d48b846-bb2d-4126-86c2-487b2244c9ad@d4g2000yqa.googlegroups.com> (raw)
In-Reply-To: 4a897b61$0$30221$9b4e6d93@newsspool1.arcor-online.net


OK, I did some more work on knuceotide.adb.  I'll say in
advance that the code may be .. a bit messy.  There is sometimes
no way around this if you are trying for fast rather than elegant.

The 1st and most important change improves running time of
the bounded string comparison operator "=".  This "=" is how the
hash routine decides if it has found the desired item, so it
could not be more important.  I'm rather proud of this because it
the speedup surprised me: apparently comparison of 32 or 64 bit ints
is a lot faster than comparing the Characters one by one in the
string.  So I used an unchecked_conversion from strings of 4
characters to 32 bit int.  The speedup was 15-20%. With this change
and with the second change described below at the end, the program
running time on my machine falls from about 53 sec to about 37 sec,
using GNAT 4.3.2 and gnatmake -O3 -gnatp.

with Unchecked_Conversion;
separate (KNucleotide_1)
package body Fragments_1 is

-- old version of "="

-- function "=" (Left, Right: Bounded_String) return Boolean is
-- begin
--      return Left.Last = Right.Last
--         and then Left.Data(1 .. Left.Last) = Right.Data(1 ..
Right.Last);
-- end "=";

-- new version of "=".  (Tell me if its right;)

   Bytes_per_Word : constant := 4;    -- 4 good, even on 64-bit archs.
   type Uns is mod 2**(8*Bytes_per_Word);
   for Uns'Size use 8*Bytes_per_Word;
   subtype Str is String(1..Bytes_per_Word);
   function To_Uns is new Unchecked_Conversion (Str, Uns);

   function "=" (Left, Right: Bounded_String) return Boolean is
      strt : Integer := 1;
      fnsh : Integer := Bytes_per_Word;
      Last : constant Integer := Left.Last;
   begin
      if Last /= Right.Last then
        return False;
      end if;

      loop
         exit when fnsh > Last;
         if To_Uns (Left.Data(strt..fnsh)) /= To_Uns (Right.Data
(strt..fnsh)) then
            return False;
         end if;
         strt := strt + Bytes_per_Word;
         fnsh := fnsh + Bytes_per_Word;
      end loop;

      for i in strt .. Last loop
         if Left.Data(i) /= Right.Data(i) then
            return False;
         end if;
      end loop;
      return True;
   end "=";

The second change gives another speedup of about half
that of the previous one.  I am shamelessly advocating
removing a layer of abstraction.  Remember, I moved  the
declaration of the bounded string to the public part, so that
the procedures can now see the 2 components of the record:
Data, and Last. Here all we do is avoid a call to
function To_Bounded_String in package Fragments_1.
Below this call is commented out:
  --
  --  Calculate the calculates the nucleotide frequencies
  --
  procedure Calculate_Frequencies (Length : Frequencies) is
     Key : Fragment;
     Len : constant Integer := Length;
  begin
     -- NEW initialization of Key.Last:
     Key.Last := Len;
     Table.Reset;
     for I in  1 .. Buffer'Last - Len + 1 loop

        -- NEW initialization of Key.Data:
        Key.Data(1 .. Len) := Buffer (I .. I + Len - 1);

        declare
         -- OLD initialization of Key.Data:
         --Key     : constant Fragment       :=
         --  Fragments_1.To_Bounded_String (Buffer (I .. I+Len-1));
           Element : constant Element_Access := Table.Get (Key);
        begin

I glance at the shootout benchmark site suggests we get a
respectable result.  Their machine is slower than mine, so
I would expect somewhere around 55 sec on their machine.
(But I am all the more impressed by the ones that are faster
than 50 sec.)  By the way, I notice that the top c++ entry
uses a static 250 megabyte array to store the data.  We
can also gain a few seconds by doing the following. (Notice
the optional 250 megabyte string. Yes, it worked.)

 procedure Read_Section is
   --Buffer     : String (1 .. 1024*1024*256); -- best; ~8% faster
than 10240
     Buffer     : String (1 .. 1024*1024*64);  -- almost as good as
above.
   --Buffer     : String (1 .. 1024*10);       -- standard setting
   --Buffer     : String (1 .. 8);             -- this works fine


Jonathan





  reply	other threads:[~2009-08-18 16:59 UTC|newest]

Thread overview: 116+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-08-01 12:21 Ada Shootout program for K-Nucleotide (patches) Georg Bauhaus
2009-08-01 12:59 ` Ludovic Brenta
2009-08-01 13:59   ` Georg Bauhaus
2009-08-01 13:50 ` Gautier write-only
2009-08-01 15:21 ` Ludovic Brenta
2009-08-01 15:37   ` Gautier write-only
2009-08-02 12:55   ` Georg Bauhaus
2009-08-27 11:17     ` Ole-Hjalmar Kristensen
2009-08-27 12:25       ` Georg Bauhaus
2009-09-01  8:06         ` Ole-Hjalmar Kristensen
2009-09-01  9:29           ` Georg Bauhaus
2009-09-01 10:48             ` Ole-Hjalmar Kristensen
2009-09-01 10:16           ` Ole-Hjalmar Kristensen
2009-09-01 11:34             ` Georg Bauhaus
2009-09-01 12:11               ` Ole-Hjalmar Kristensen
2009-09-01 14:36                 ` Georg Bauhaus
2009-09-03  8:49                   ` Ole-Hjalmar Kristensen
2009-09-02  8:44                 ` Georg Bauhaus
2009-09-02 11:07                   ` Ole-Hjalmar Kristensen
2009-09-03  8:13                   ` Ole-Hjalmar Kristensen
2009-09-03 10:39                     ` Georg Bauhaus
2009-08-03  8:56   ` Jacob Sparre Andersen
2009-08-03 11:43     ` Georg Bauhaus
2009-08-03 12:36       ` Jacob Sparre Andersen
2009-08-03 18:31         ` Isaac Gouy
2009-08-03 20:29           ` Robert A Duff
2009-08-04 15:43             ` Isaac Gouy
2009-08-04 16:06               ` Isaac Gouy
2009-08-04 17:08                 ` Georg Bauhaus
2009-08-05 15:58                   ` Isaac Gouy
2009-08-05 21:18                     ` Georg Bauhaus
2009-08-05 22:04                       ` Ludovic Brenta
2009-08-05 22:31                         ` Georg Bauhaus
2009-08-05 23:44                           ` Jeffrey R. Carter
2009-08-06  8:38                             ` Georg Bauhaus
2009-08-06 21:39                               ` Georg Bauhaus
2009-08-06 22:42                                 ` Jeffrey R. Carter
2009-08-07  8:53                                   ` Georg Bauhaus
2009-08-07  9:21                                     ` Jacob Sparre Andersen
2009-08-07  9:23                                       ` Jacob Sparre Andersen
2009-08-09 19:17                                       ` Georg Bauhaus
2009-08-13 20:56                                 ` jonathan
2009-08-13 21:30                                   ` jonathan
2009-08-13 22:08                                   ` Georg Bauhaus
2009-08-14 15:31                                     ` jonathan
2009-08-06  7:51                           ` Dmitry A. Kazakov
2009-08-06  0:07                       ` Isaac Gouy
2009-08-06  8:36                         ` Georg Bauhaus
2009-08-06  9:09                           ` Dmitry A. Kazakov
2009-08-06 10:34                             ` Georg Bauhaus
2009-08-06 10:49                               ` Dmitry A. Kazakov
2009-08-06 15:21                                 ` Isaac Gouy
2009-08-03 20:47           ` Ludovic Brenta
2009-08-01 17:07 ` jonathan
2009-08-01 17:59 ` jonathan
2009-08-02 11:39   ` Georg Bauhaus
2009-08-02 16:00     ` jonathan
2009-08-02 16:42       ` jonathan
2009-09-03 13:44     ` Olivier Scalbert
2009-09-03 14:08       ` Ludovic Brenta
2009-09-03 15:13         ` Olivier Scalbert
2009-09-03 19:11           ` sjw
2009-09-04  6:11             ` Olivier Scalbert
2009-09-04  8:18               ` Ludovic Brenta
2009-09-04 10:17                 ` Olivier Scalbert
2009-09-04 12:37                   ` Ludovic Brenta
2009-09-04 13:50                     ` Olivier Scalbert
2009-09-04 12:50                   ` Ludovic Brenta
2009-09-04 16:22                     ` Georg Bauhaus
2009-09-04 16:29                       ` Georg Bauhaus
2009-09-04 16:38                         ` Ludovic Brenta
2009-09-04 17:58                           ` Georg Bauhaus
2009-09-04 18:12                             ` Georg Bauhaus
2009-09-05 20:16                             ` Georg Bauhaus
2009-09-07  7:45                           ` Olivier Scalbert
2009-09-07  9:19                             ` Georg Bauhaus
2009-09-07 13:31                               ` Olivier Scalbert
2009-09-07 14:38                                 ` jonathan
2009-09-07 15:03                                   ` Olivier Scalbert
2009-09-07 17:31                                     ` jonathan
2009-09-07 17:54                                       ` Olivier Scalbert
2009-09-07 18:07                                     ` Georg Bauhaus
2009-09-08  0:22                                 ` Georg Bauhaus
2009-09-04 17:56                     ` Martin
2009-09-04 18:26                       ` Georg Bauhaus
2009-09-04 21:51                         ` Robert A Duff
2009-09-04 18:51                       ` Ludovic Brenta
2009-09-04  9:27               ` Georg Bauhaus
2009-09-03 15:28       ` Georg Bauhaus
2009-09-04  1:24         ` Georg Bauhaus
2009-08-04  8:23 ` Martin
2009-08-16 15:20 ` jonathan
2009-08-17 15:46   ` Georg Bauhaus
2009-08-18 16:59     ` jonathan [this message]
2009-08-19 14:52       ` Georg Bauhaus
2009-08-19 16:40         ` Martin
2009-08-19 18:24           ` Robert A Duff
2009-08-19 22:15             ` jonathan
2009-08-19 23:50               ` Georg Bauhaus
2009-08-20 19:47                 ` jonathan
2009-08-20 22:15                   ` Georg Bauhaus
2009-08-21 21:43                     ` jonathan
2009-08-22 22:35                       ` Georg Bauhaus
2009-08-23 22:21                         ` jonathan
2009-08-20 19:55                 ` jonathan
2009-08-19 21:37           ` Georg Bauhaus
2009-08-19 19:22         ` jonathan
2009-08-19 19:27         ` jonathan
2009-08-27  9:05 ` Martin
2009-08-27  9:08   ` Martin
2009-08-27 10:01     ` Georg Bauhaus
2009-10-07 11:44 ` Olivier Scalbert
2009-10-07 12:04   ` Georg Bauhaus
2009-10-07 13:22   ` Martin
2009-10-07 14:15     ` Olivier Scalbert
2009-10-08  9:54       ` Georg Bauhaus
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox