comp.lang.ada
 help / color / mirror / Atom feed
* Stack based allocation vs. Dynamic allocation
@ 2000-05-31  0:00 dale
  2000-05-31  0:00 ` Ray Blaak
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: dale @ 2000-05-31  0:00 UTC (permalink / raw)


A discussion at work left me claiming that stack based allocation
was quicker than heap based allocation. 

A person wrote up a demonstration program that proved that this
wasn't the case (at least for the experiment).

The code (translated to Ada by me) is...

procedure Stack is

   N : constant := 500;

   Big : constant := 1024 ** 2;

   type ints is array (0 .. Big) of Integer;

begin
   for i in 0 .. N - 1 loop
      declare
         a : Ints;

      begin
         for j in a'range loop
            a(j) := j;
         end loop;
      end;
   end loop;
end;



with unchecked_deallocation;

procedure Heap is
   N : constant := 500;

   Big : constant := 1024 ** 2;


   type ints is array (0 .. N - 1) of integer;
   type ints_ptr is access ints;

   procedure free is new unchecked_deallocation (ints, ints_ptr);

begin


   for i in 0 .. N - 1 loop
      declare
         a : ints_Ptr := new ints;

      begin
         for j in a'range loop
            a(j) := j;
         end loop;

         free (a);
      end;
   end loop;
end;


The results show that the heap allocation is considerably faster
everytime (that i ran it).

Using gnat 3.12, -O2, Solaris consistently gives me the results 
very similar to the following...

> time ./heap
0.01u 0.02s 0:00.18 16.6%
> time ./stack
17.72u 0.10s 0:19.86 89.7%


Does anyone know what the factors are that would cause stack
allocation to be so slow?


Dale




^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2000-06-05  0:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-05-31  0:00 Stack based allocation vs. Dynamic allocation dale
2000-05-31  0:00 ` Ray Blaak
2000-05-31  0:00   ` Dale Stanbrough
2000-05-31  0:00     ` Laurent Guerby
2000-06-01  0:00       ` Matthew Woodcraft
2000-06-01  0:00         ` Laurent Guerby
2000-06-05  0:00     ` Robert Dewar
2000-05-31  0:00   ` Gisle S�lensminde
2000-05-31  0:00     ` Lutz Donnerhacke
2000-05-31  0:00       ` Gisle S�lensminde
2000-05-31  0:00     ` Aaro Koskinen
2000-05-31  0:00 ` Jean-Pierre Rosen
2000-05-31  0:00 ` Aaro Koskinen

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