From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,123c40d62c632159,start X-Google-Attributes: gid103376,public From: dale Subject: Stack based allocation vs. Dynamic allocation Date: 2000/05/31 Message-ID: #1/1 X-Deja-AN: 629335564 X-Trace: 31 May 2000 16:05:50 +1000, dale.cs.rmit.edu.au Organization: rmit User-Agent: MT-NewsWatcher/3.0 (PPC) Newsgroups: comp.lang.ada Date: 2000-05-31T00:00:00+00:00 List-Id: 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