Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Cell-Array preallocation slows code down

Subject: Cell-Array preallocation slows code down

From: Vincent

Date: 5 Nov, 2012 13:35:10

Message: 1 of 2

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?

Subject: Cell-Array preallocation slows code down

From: Yair Altman

Date: 28 Nov, 2012 22:16:19

Message: 2 of 2

"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
 

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us