From: "Steve Amphlett" <me@home.now>
Path: news.mathworks.com!newsfeed-00.mathworks.com!webx
Newsgroups: comp.soft-sys.matlab
Subject: Re: growing array in Matlab?
Message-ID: <ef11420.8@webx.raydaftYaTP>
Date: Wed, 17 Aug 2005 15:12:40 -0400
References: <ef11420.0@webx.raydaftYaTP> <ef11420.1@webx.raydaftYaTP> <A4JMe.327$n5.350@mencken.net.nih.gov> <ef11420.3@webx.raydaftYaTP> <woodchips-384047.13045317082005@syrcnyrdrs-02-ge0.nyroc.rr.com>
Lines: 48
NNTP-Posting-Host: 62.255.64.10
MIME-Version: 1.0
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
Xref: news.mathworks.com comp.soft-sys.matlab:296476


John D'Errico wrote:
>
>
> I've played with this a bit at times. See my other
> post in this thread. A problem is with a huge number
> of appends. In some of my codes, I may need to append
> potentially millions of times. When you try the above
> codes for large numbers of appends then things get
> interesting.
>
> x=2;
> clear b
> tic
> for idx=1:20000
> b(idx) = {sin(x)};
> end
> c=cell2mat(b);
> toc
>
>
> clear b
> tic
> for idx=1:40000
> b(idx) = {sin(x)};
> end
> c=cell2mat(b);
> toc
>
>
> Elapsed time is 4.547330 seconds.
> Elapsed time is 17.345010 seconds.
>
>
> Note that doubling the loop index does not double
> the time.
>
> The cell array approach works very well for small
> enough numbers of cells. The block zero appends work
> well as long as you don't need to re-allocate that
> exponentially growing array too often. This can
> cause your machine to wander into virtual memory -
> a very bad thing for speed.

My observation was that the bigger the appended items get, the better
the cell approach is. You're always going to be reallocating (new
memory followed by copying data) in any extension mode. Unless TMW
can do a linked-list cell array implementation. That would kick!
(1D easy, but what about more than that?)