comp.lang.ada
 help / color / mirror / Atom feed
From: dale <dale@cs.rmit.edu.au>
Subject: Stack based allocation vs. Dynamic allocation
Date: 2000/05/31
Date: 2000-05-31T00:00:00+00:00	[thread overview]
Message-ID: <dale-D70BFC.16062031052000@news.rmit.edu.au> (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




             reply	other threads:[~2000-05-31  0:00 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-05-31  0:00 dale [this message]
2000-05-31  0:00 ` Stack based allocation vs. Dynamic allocation Ray Blaak
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   ` 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 ` Jean-Pierre Rosen
2000-05-31  0:00 ` Aaro Koskinen
replies disabled

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