comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam.jrcarter.not@spam.not.acm.org>
Subject: Re: Numerical calculations: Why not use fixed point types for everything?
Date: Fri, 18 Jan 2013 23:26:25 -0700
Date: 2013-01-18T23:26:25-07:00	[thread overview]
Message-ID: <kddeai$f9i$1@dont-email.me> (raw)
In-Reply-To: <sf8kf8pmgjd69d20pfm3qtahmd2t6nkfrv@invalid.netcom.com>

On 01/18/2013 09:41 PM, Dennis Lee Bieber wrote:
>
> 	My emphasis was that, lacking true hardware fixed point operations,
> the compiler has to track the position of the implied point and insert
> operations to (for lack of better term) normalize the results back into
> the declared type. Those the negative impact of those normalization
> instructions may cancel out any expected gain from using integer
> arithmetic operations.

I decided to run a test. My results:

jrcarter@jrcarter-gateway-1:~/Code$ ./sum
Float took 0.007979000
Fixed took 0.008180000
jrcarter@jrcarter-gateway-1:~/Code$ ./sum
Float took 0.007971000
Fixed took 0.008278000
jrcarter@jrcarter-gateway-1:~/Code$ ./sum
Float took 0.007979000
Fixed took 0.008403000

It seems that floating-point operations are slightly faster than fixed point on 
a (sort of) modern processor (AMD Athlon II M300).

The code:

with Ada.Real_Time;
with Ada.Text_IO;

with PragmARC.Universal_Random;

procedure Sum is
    package Random is new PragmARC.Universal_Random (Supplied_Real => Float);

    procedure Process is
       Max : constant := 5_000.0;

       subtype Index_Value is Integer range 1 .. 1_024;

       subtype Value is Float range -Max .. Max;

       type Value_List is array (Index_Value range <>) of Value;

       type Fixed is delta 2.0 ** (-20) range -Max .. Max;

       type Fixed_List is array (Index_Value range <>) of Fixed;

       Value_Length : constant Value := Value (Index_Value'Last);

       function Sum (List : in Value_List) return Value is
          Result : Value := 0.0;
       begin -- Sum
          Add_All : for I in List'Range loop
             Result := Result + List (I) / Value_Length;
          end loop Add_All;

          return Result;
       end Sum;

       Length : constant Index_Value := Index_Value'Last;

       function Sum (List : in Fixed_List) return Fixed is
          Result : Fixed := 0.0;
       begin -- Sum
          Add_All : for I in List'Range loop
             Result := Result + List (I) / Length;
          end loop Add_All;

          return Result;
       end Sum;

       Value_Set : Value_List (Index_Value) := (Index_Value => 
Random.Random_Range (-Max, Max) );
       Fixed_Set : Fixed_List (Index_Value);

       Target_Index : constant Index_Value := Random.Random_Int 
(Index_Value'First, Index_Value'Last);

       Value_Result : Value;
       Fixed_Result : Fixed;
       Start        : Ada.Real_Time.Time;

       use type Ada.Real_Time.Time;
    begin -- Process
       Copy : for I in Value_Set'Range loop
          Fixed_Set (I) := Fixed (Value_Set (I) );
       end loop Copy;

       Start := Ada.Real_Time.Clock;

       Float_Test : for I in Index_Value loop
          Value_Result := Sum (Value_Set);
       end loop Float_Test;

       Ada.Text_IO.Put_Line (Item => "Float took" & Duration'Image 
(Ada.Real_Time.To_Duration (Ada.Real_Time.Clock - Start) ) );

       Start := Ada.Real_Time.Clock;

       Fixed_Test : for I in Index_Value loop
          Fixed_Result := Sum (Fixed_Set);
       end loop Fixed_Test;

       Ada.Text_IO.Put_Line (Item => "Fixed took" & Duration'Image 
(Ada.Real_Time.To_Duration (Ada.Real_Time.Clock - Start) ) );
    end Process;
begin -- Sum
    Random.Randomize;
    Process;
end Sum;

(I realize this actually calculates averages, but it started out doing sums 
until I realized the sum might not fit into the ranges I was using, and I'm too 
lazy to change the names.)

-- 
Jeff Carter
"I wave my private parts at your aunties."
Monty Python & the Holy Grail
13



  reply	other threads:[~2013-01-19  6:26 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-17 10:33 Numerical calculations: Why not use fixed point types for everything? Ada novice
2013-01-17 14:40 ` Nasser M. Abbasi
2013-01-17 16:16   ` Adam Beneschan
2013-01-17 17:00     ` Georg Bauhaus
2013-01-17 16:25 ` Adam Beneschan
2013-01-18  9:17   ` Ada novice
2013-01-18 17:24     ` J-P. Rosen
2013-01-18 17:52       ` Jeffrey Carter
2013-01-18 18:15     ` Dennis Lee Bieber
2013-01-18 18:59       ` Adam Beneschan
2013-01-19  4:41         ` Dennis Lee Bieber
2013-01-19  6:26           ` Jeffrey Carter [this message]
2013-01-19 14:14             ` Robert A Duff
2013-01-25 12:16               ` Paul Colin Gloster
2013-01-24 10:55             ` Ada novice
2013-01-24 11:47               ` Simon Wright
2013-01-24 14:21                 ` Ada novice
2013-01-20  0:05           ` Robin Vowels
2013-01-18 23:06       ` Robin Vowels
2013-01-18 19:09     ` Adam Beneschan
2013-01-18 21:39       ` Randy Brukardt
2013-01-19  7:02         ` Ada novice
2013-01-25 12:09         ` Paul Colin Gloster
2013-01-25 12:23     ` Paul Colin Gloster
2013-01-28  9:09       ` Ada novice
2013-02-01 10:53         ` Ada novice
2013-02-01 15:01           ` Shark8
2013-02-02 18:55             ` Ada novice
2013-02-03  4:05               ` Shark8
2013-02-04  6:23                 ` Ada novice
2013-02-04  6:43                   ` Niklas Holsti
2013-02-04  7:27                     ` Ada novice
2013-02-04  9:37                       ` Niklas Holsti
2013-02-04 10:09                         ` Ada novice
2013-02-04 14:24                           ` Niklas Holsti
2013-02-04 16:44                             ` Jeffrey Carter
2013-02-04 21:12                               ` Niklas Holsti
2013-02-04 17:31                             ` Robert A Duff
2013-02-04 21:20                               ` Niklas Holsti
2013-02-02 21:08           ` Nasser M. Abbasi
2013-02-04  6:17             ` Ada novice
2013-02-05  2:27               ` Randy Brukardt
2013-02-06  7:11                 ` Ada novice
2013-02-07  6:03                   ` Randy Brukardt
2013-02-07  8:43                     ` Shark8
2013-02-08  3:17                       ` Randy Brukardt
2013-02-08  6:20                     ` Ada novice
replies disabled

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