Cell-Preallocation slows down code

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 个评论

Without any code its really hard to guess what's going on. How did you preallocate? And how big is the cell array?
Good point. I have 3000 files and estimated 5000 columns. One of these is my preallocation (same order as above):
mdb = cell(0,0);
mdb = cell(size(files,1),0);
mdb = cell(size(files,1),5000);
Matt J
Matt J 2012-11-5
编辑:Matt J 2012-11-5
What are you putting into the cells? Show us the code for that.
Well as I said, my data is always a character array. When inserting I use this code:
mdb{row,col} = 'texty text';
This happens within a for-loop. It has to be said, that all those actions appear within an object (e.g. obj.mdb{3,5} = data). And I am using Matlab R2007b. The object holds also a numeric array (didn't tell about this for simplicity). However in this case preallocation doesn't result in such bad perfomance
Is it a handle class?
Vincent
Vincent 2012-11-5
编辑:Vincent 2012-11-5
No it isn't. Perhaps I'll explain what I intend to do: I have a bunch of ini-files. I have an object IniConfig reading those files into a Database-class called MixDatabase. IniConfig is a handle-class, however the profiler shows the slow access within the MixDatabase-object. The Database has these arrays:
  1. Head-Array: containing the Section and Key of each ini-Entry
  2. Float-Array: contaiing all numeric values within the ini-Files
  3. Cell-Array: containing all string values within the ini-Files
  4. Link-Array: assigns each entry in the head-array an entry of the float/cell-Array
The preallocation was used with the Float-Array, Cell-Array and Head-Array. The latter ones had these performance problems.
I'm sorry that I didn't write all this into the initial question, but it would have distracted from the important facts I think.

请先登录,再进行评论。

 采纳的回答

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 个评论

I hope I understood your answer correctly. I will access each cell of the array exactly once. Is there anything I can do to speed the process up (also in OOP)?
In order to speed things up, you need to understand where the slow down is. I am suggesting the slow down is from accessing a large cell array. Try to access the same element over and over again and see if the size of the cell array has an effect.
Yes, it does. With the big preallocation, I have access times much longer than with the "little" preallocation (>80 times longer). So propaply I'll split the big cell-Array up and merge it lateron or are there other usefuls tricks?
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 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by