Cell-Preallocation slows down code
12 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Daniel Shub
2012 年 11 月 5 日
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 件のコメント
Daniel Shub
2012 年 11 月 5 日
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!