comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <bauhaus@futureapps.de>
Subject: Re: GNAT compiler switches and optimization
Date: Mon, 23 Oct 2006 02:10:53 +0200
Date: 2006-10-23T02:08:39+02:00	[thread overview]
Message-ID: <1161562253.5362.82.camel@localhost.localdomain> (raw)
In-Reply-To: <2jps04-ggi.ln1@newserver.thecreems.com>

On Sun, 2006-10-22 at 14:24 -0400, Jeffrey Creem wrote:

> It sounds like you are running some different code and I'd be hestitant 
> to make any assertions about runtime for certain constructs without 
> seeing it since

It is appended below.

> 2) You mention something about just accessing first, middle and last of 
> your arrays so it really sounds like you really are just timing 
> allocations and and not actually really hitting the array indexing 
> (though hard to say without seeing the code).

Yes, I had concentrated on just this question, asked by Thomas Krauss,
about stack versus heap allocation. If a program is not frequently
allocating and freeing arrays (or matrices, etc.), the the effects
might be less of an issue, if they are an issue at all when there
is no initialization.
But I imagined allocation is just what is happening all the time
if you have a function that computes matrices of varying sizes?
 OTOH, I find very different results on GNU/Linux (for which I only
have a number of GNATs). Stack allocation appears to be consuming
next to no time. (Commit on write? Just remembering an offset?)

Below the following program, there is a patch that adds trivial
initialization loops. With them, a comparison of stack vs heap
on GNU/Linux seems in favor of the stack. I can't check this on
Windows right now, though.

with Ada.Calendar;
with Ada.Text_IO;
with Ada.Unchecked_Deallocation;

procedure main is
   use Ada.Calendar, Ada;

   type LIST is array (NATURAL range <>) of BOOLEAN;
   for LIST'component_size use 8;
      -- for a "normal" array, not packed or other magic

   type LIST_PTR is access LIST;
   procedure free is new Ada.Unchecked_Deallocation
      (LIST, LIST_PTR);

   accu: BOOLEAN := false;
      -- The allocating functions read and write this variable
      -- using components of the local arrays.
      -- (This should prevent some optimizations.)

   function allocate(size: POSITIVE) return BOOLEAN;
     -- use a local `LIST` of length `size`

   function allocate_heap(size: POSITIVE) return BOOLEAN;
      -- use a pointer to a new `LIST` of length `size`

   function image(t: TIME) return STRING;
      -- the current time as a `STRING` value
   

   function allocate(size: POSITIVE) return BOOLEAN is
         done: LIST(1 .. size);  -- pragma volatile(done);
         result: BOOLEAN;
      begin
         if done(size / 2) then
            result := false;
         end if;
         done(done'last) := accu and done(done'first);
         result := done(done'last) and done(done'first);
         return result;
      end allocate;

   function allocate_heap(size: POSITIVE) return BOOLEAN is
         done: LIST_PTR;
         result: BOOLEAN;
      begin
         done := new LIST(1 .. size);
         if done(size / 2) then
            result := false;
         end if;
         done(done'last) := accu and done(done'first);
         result := done(done'first) and done(done'first);
         Free(done);
         return result;
      end allocate_heap;

   function image(t: TIME) return STRING is
         year: YEAR_NUMBER;
         day: DAY_NUMBER;
         month: MONTH_NUMBER;
         sec: DURATION;
      begin
         split(t, year, month, day, sec);
         return YEAR_NUMBER'image(year)
           & MONTH_NUMBER'image(month)
           & DAY_NUMBER'image(day)
           & DURATION'image(sec);
      end image;


   start, finish: TIME;
   

begin
   Text_IO.put_line("using a stack");
   start := clock;
   for run in 1 .. 25 loop
      for k in 1 .. 2000 loop
         accu := allocate(5000 * k);
      end loop;
   end loop;
   finish := clock;
   
   Text_IO.put_line("from" & image(start)
         & " to" & image(finish)
         & " = " & DURATION'image(finish - start));
   Text_IO.put_line("accu " & BOOLEAN'image(accu));


   Text_IO.put_line("using a heap");
   start := clock;
   for run in 1 .. 25 loop
      for k in 1 .. 2000 loop
         accu := allocate_heap(5000 * k);
      end loop;
   end loop;
   finish := clock;

   Text_IO.put_line("from" & image(start)
         & " to" & image(finish)
         & " = " & DURATION'image(finish - start));
   Text_IO.put_line("accu " & BOOLEAN'image(accu));
end main;



--- stack_use_testing.ada       2006/10/22 23:51:48     1.1
+++ stack_use_testing.ada       2006/10/22 23:52:12
@@ -33,2 +33,3 @@
       begin
+         for k in done'range loop done(k) := false; end loop;
          if done(size / 2) then
@@ -46,2 +47,3 @@
          done := new LIST(1 .. size);
+         for k in done'range loop done(k) := false; end loop;
          if done(size / 2) then
@@ -75,4 +77,4 @@
    start := clock;
-   for run in 1 .. 25 loop
-      for k in 1 .. 2000 loop
+   for run in 1 .. 2 loop
+      for k in 1 .. 200 loop
          accu := allocate(5000 * k);
@@ -90,4 +92,4 @@
    start := clock;
-   for run in 1 .. 25 loop
-      for k in 1 .. 2000 loop
+   for run in 1 .. 2 loop
+      for k in 1 .. 200 loop
          accu := allocate_heap(5000 * k);





  reply	other threads:[~2006-10-23  0:10 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-10-20 10:47 GNAT compiler switches and optimization tkrauss
2006-10-20 11:04 ` Duncan Sands
2006-10-21 10:45   ` Stephen Leake
2006-10-20 11:42 ` Duncan Sands
2006-10-20 15:41   ` Martin Krischik
2006-10-20 12:09 ` Samuel Tardieu
2006-10-20 12:18   ` Samuel Tardieu
2006-10-20 12:12 ` Gautier
2006-10-20 12:35 ` Dmitry A. Kazakov
2006-10-20 15:53   ` Martin Krischik
2006-10-20 12:52 ` Gautier
2006-10-20 13:27 ` claude.simon
2006-10-20 15:38 ` Robert A Duff
2006-10-20 19:32   ` Gautier
2006-10-20 15:56 ` Jeffrey Creem
2006-10-20 16:30 ` Martin Krischik
2006-10-20 19:51 ` Gautier
2006-10-20 22:11 ` Jeffrey R. Carter
2006-10-20 23:52   ` Jeffrey Creem
2006-10-21  7:37     ` Gautier
2006-10-21 16:35       ` Jeffrey Creem
2006-10-21 17:04         ` Pascal Obry
2006-10-21 21:22           ` Jeffrey Creem
2006-10-22  3:03             ` Jeffrey Creem
2006-10-22  7:39               ` Jeffrey R. Carter
2006-10-22 11:48                 ` tkrauss
2006-10-22 18:02                   ` Georg Bauhaus
2006-10-22 18:24                     ` Jeffrey Creem
2006-10-23  0:10                       ` Georg Bauhaus [this message]
2006-10-22 20:20                   ` Jeffrey R. Carter
2006-10-22 12:31                 ` Gautier
2006-10-22 20:26                   ` Jeffrey R. Carter
2006-10-22 21:22                     ` Gautier
2006-10-22 18:01                 ` tmoran
2006-10-22 20:54                   ` Jeffrey R. Carter
2006-10-22 13:50               ` Alinabi
2006-10-22 15:41                 ` Jeffrey Creem
2006-10-23  0:02                   ` Alinabi
2006-10-23  5:28                     ` Gautier
2006-10-23 16:32                       ` Alinabi
2006-10-22 15:57               ` Jeffrey Creem
2006-10-22 19:32                 ` Damien Carbonne
2006-10-22 20:00                   ` Gautier
2006-10-22 20:51                     ` Damien Carbonne
2006-10-23  2:15                       ` Jeffrey Creem
2006-10-23  2:29                         ` Jeffrey R. Carter
2006-10-23  1:31                   ` Jeffrey Creem
2006-10-23  3:10                     ` Jeffrey Creem
2006-10-23  7:31                       ` Jeffrey R. Carter
2006-10-23 11:55                         ` Jeffrey Creem
2006-10-23 19:52                           ` Wiljan Derks
2006-10-23 20:25                             ` Jeffrey R. Carter
2006-10-24  9:52                             ` Dr. Adrian Wrigley
2006-10-24 11:50                               ` Jeffrey Creem
2006-10-24 16:24                                 ` Jeffrey R. Carter
2006-10-25  3:50                                   ` Jeffrey Creem
2006-10-25 15:32                                     ` claude.simon
2006-10-24 19:21                               ` Wiljan Derks
2006-10-23 12:33                   ` Warner BRUNS
2006-10-23 12:40                   ` Warner BRUNS
2006-10-23 13:52                     ` Georg Bauhaus
2006-10-23 17:11                       ` Warner BRUNS
2006-10-23 17:57                         ` Dr. Adrian Wrigley
2006-10-23 15:02                     ` Robert A Duff
2006-10-23 20:22                       ` Jeffrey R. Carter
2006-10-21 18:28         ` tmoran
2006-10-23  6:28       ` Martin Krischik
2006-10-21 12:39 ` Dr. Adrian Wrigley
replies disabled

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