Path: news.mathworks.com!not-for-mail
From: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Cell-Array preallocation slows code down
Date: Wed, 28 Nov 2012 22:16:19 +0000 (UTC)
Organization: TACT Computer Systems Ltd
Lines: 19
Message-ID: <k962fj$c0o$1@newscl01ah.mathworks.com>
References: <k78fae$gfp$1@newscl01ah.mathworks.com>
Reply-To: "Yair Altman" <altmanyDEL@gmailDEL.comDEL>
NNTP-Posting-Host: www-03-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1354140979 12312 172.30.248.48 (28 Nov 2012 22:16:19 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 28 Nov 2012 22:16:19 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 642467
Xref: news.mathworks.com comp.soft-sys.matlab:783756

"Vincent" wrote in message <k78fae$gfp$1@newscl01ah.mathworks.com>...
> I created an object containing two cell-Arrays. These cell-Arrays will be filled with file contents. I do know how many rows the cell results in, however I can only estimate the columns.
> 
> As I read in the help topics, preallocation could speed up my code. So I tried it and these are the results after profiling during the data insertion
> 
> Without preallocation: 0.4 ms
> With row-preallocation: 9 ms
> With row and estimated column-preallocation: 95 ms
> 
> How is this even possible? I'd like to optimize my code further. I could even assign the datatype for the cell-Array (char) - can I use this information?


Try out the different alternatives to preallocation mentioned in the following articles:
http://UndocumentedMatlab.com/blog/preallocation-performance/
http://UndocumentedMatlab.com/blog/array-resizing-performance/

Yair Altman 
http://UndocumentedMatlab.com