Re: g_realloc (OFF-TOPIC) (part 2)



On Thu, 26 Oct 2000, Jose Eusebio Roza Pando wrote:

Jose Eusebio Roza Pando escribió:

Hi ,
   Does anyone know is the processing time for increasing 1 in
an array with g_realloc depends of array size ?
if array has 10 elements the time is bigger than
array has 100000 elements?
Thanks for any help.
   Eusebio


Opps! I mean if array has 100000 elements the time is bigger than
array has 10 elements?

AFAIK, standard malloc/realloc algorithms iterate through the available
chunks of memory to find one that has the right size. For realloc, they
first try to allocate a chunck just after the array you want to increase
in size. As long as you have enough memory, and as long as it is not
fragmented, it is the same thing for both arrays. However, you should note
that realloc'ing elements one by one for a 100000 elements array in _not_
efficient. You should at least try to guess what the size of the array
will be so that your initial allocation will be ok in most cases. If you
really have no way to guess (you want to implement a generic container,
for instance), then what is generally done (in python lists, in Java
vectors...) is to double the size of the array each time you need to
realloc: this doesn't spoil too much memory for small arrays, and limits
the fragmentation of the memory for big arrays.


-- 
Alexandre Fayolle
http://www.logilab.com 
LOGILAB, Paris (France).





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]