Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Tuple allocation overhead
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-08-12 (16:09)
From: Raj Bandyopadhyay <technophobicgeek@g...>
Subject: Tuple allocation overhead
Hi folks

I was performing a little experiment to try and quantify some of the 
overhead of memory allocation in OCaml. Here is a the kind of little 
program that I am using for timings:

let rec alloc3 n l =
    if n = 0 then l
    else alloc3 (n-1) ((1,2,n)::l)

The alloc3 function allocates a tuple of size 3 at every call (n is 
initially 100,000).

I have similar functions alloc1 through alloc10, to allocate tuples of 
size 1 (just an integer) through 10.
Here are my timing results (each time is the average of 100 runs):

 __Tuple sz 1 ________________ 100x avg= 1.964404E+01 msec
__ Tuple sz 2 ________________ 100x avg= 4.699384E+01 msec
__ Tuple sz 3 ________________ 100x avg= 4.232895E+01 msec
__ Tuple sz 4 ________________ 100x avg= 4.265683E+01 msec
__ Tuple sz 5 ________________ 100x avg= 4.442121E+01 msec
__ Tuple sz 6 ________________ 100x avg= 4.723042E+01 msec
__ Tuple sz 7 ________________ 100x avg= 5.168987E+01 msec
__ Tuple sz 8 ________________ 100x avg= 5.234274E+01 msec
__ Tuple sz 9 ________________ 100x avg= 5.504287E+01 msec
__ Tuple sz 10 _______________ 100x avg= 5.836166E+01 msec

As expected, the larger the size of the tuple, the longer it takes. 
However, there is a spike for tuple size 2. I am seeing this on multiple 
platforms (linux and mac).

I was wondering if someone has a good explanation for this. I assume it 
has to do with some overhead of malloc, but I'm not sure. Why is it that 
a tuple of size 2 is more expensive to allocate than size 3-5 in this 
case? Are there other factors involved that I am not considering?