Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Cell-Array preallocation slows code down
Date: Mon, 5 Nov 2012 13:35:10 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 9
Message-ID: <k78fae$gfp$1@newscl01ah.mathworks.com>
Reply-To: <HIDDEN>
NNTP-Posting-Host: www-01-blr.mathworks.com
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: newscl01ah.mathworks.com 1352122510 16889 172.30.248.46 (5 Nov 2012 13:35:10 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Mon, 5 Nov 2012 13:35:10 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2984401
Xref: news.mathworks.com comp.soft-sys.matlab:782054

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?