help-octave
[Top][All Lists]
Advanced

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

dynamic allocation of cell arrays is sloooow


From: Francesco Potortì
Subject: dynamic allocation of cell arrays is sloooow
Date: Wed, 16 Mar 2016 12:17:48 +0100

Dynamic allocation of array-like data structures is inherently
inefficient, but sometimes it vastly simplifies coding, especially when
the total size of the data structure is unknown in advance.

Octave does a good job at doing dynamic allocation of arrays, but is
apparently very inefficient when cell arrays or struct arrays are
involved.  Here is a simple benchmark:

>> version
ans = 4.0.0

>> a=[]; for esp=1:6; tic; for i=1:10^esp; a(i)=i; end; toc; end
Elapsed time is 0.000110149 seconds.
Elapsed time is 0.000464916 seconds.
Elapsed time is 0.00417209 seconds.
Elapsed time is 0.040483 seconds.
Elapsed time is 0.417366 seconds.
Elapsed time is 5.72097 seconds.

>> a={}; for esp=1:6; tic; for i=1:10^esp; a{i}=i; end; toc; end
Elapsed time is 0.000128031 seconds.
Elapsed time is 0.000672817 seconds.
Elapsed time is 0.00595999 seconds.
Elapsed time is 0.063189 seconds.
Elapsed time is 1.49592 seconds.
Elapsed time is 145.996 seconds.

>> a=struct("a",{}); for esp=1:6; tic; for i=1:10^esp; a(i).a=i; end; toc; end
Elapsed time is 0.00019598 seconds.
Elapsed time is 0.140211 seconds.
Elapsed time is 0.0078249 seconds.
Elapsed time is 0.0820489 seconds.
Elapsed time is 1.82587 seconds.
Elapsed time is 158.164 seconds.

I find myself in the latter case: a struct array with around a million
entries.  The only reasonable way I found is to do excess initial
allocation and then delete the excess entries at the end.  Is this the
recommended way?

-- 
Francesco Potortì (ricercatore)        Voice:  +39.050.621.3058
ISTI - Area della ricerca CNR          Mobile: +39.348.8283.107
via G. Moruzzi 1, I-56124 Pisa         Skype:  wnlabisti
(entrance 20, 1st floor, room C71)     Web:    http://fly.isti.cnr.it




reply via email to

[Prev in Thread] Current Thread [Next in Thread]