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$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1352122510 16889 (5 Nov 2012 13:35:10 GMT)
NNTP-Posting-Date: Mon, 5 Nov 2012 13:35:10 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 2984401
Xref: 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?