Cell-Preallocation slows down code
11 views (last 30 days)
Show older comments
I have an object containing two cell-Arrays. I know the number of rows but I can only estimate the column-size.
When preallocation the cell-Array (which will contain only chars afterwards), I get way slower code, which I don't understand:
- No Preallocation: 0.4ms per Insertion
- Row Preallocation: 9.8 ms
- Row Preallocation and Estimation of Columns: 95.0 ms
I didn't finish my code, so perhaps the speedup comes at the end? I cannot explain this strange behaviour...
6 Comments
Accepted Answer
Daniel Shub
on 5 Nov 2012
Preallocating a cell array is of limited benefit since it only contains pointers to the memory locations of the elements. I think the OO system is slow to access your large cell array such that the benefits of preallocation are swamped by slow access. My guess is that if you call
obj.mdb{3,5} = 'texty text';
a bunch of times with the same indices that you will see differences between preallocating the array to be
mdb = cell(1000, 5000);
and
mdb = cell(10, 50);
5 Comments
Daniel Shub
on 5 Nov 2012
I would think that insight about preallocation/big array access might be worth an upvote (or maybe even an accepted answer). I think you could now ask a new question with a MWE (minimum working example). Ideally you would showing the problem to be access speed to large cell arrays within an object and that the problem doesn't happen when it is just a regular cell array.
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!