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

Arthur
Arthur 2012년 11월 5일
Without any code its really hard to guess what's going on. How did you preallocate? And how big is the cell array?
Vincent
Vincent 2012년 11월 5일
편집: Vincent 2012년 11월 5일
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.
Vincent
Vincent 2012년 11월 5일
편집: Vincent 2012년 11월 5일
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
Matt J
Matt J 2012년 11월 5일
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

Daniel Shub
Daniel Shub 2012년 11월 5일

1 개 추천

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

Vincent
Vincent 2012년 11월 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)?
Daniel Shub
Daniel Shub 2012년 11월 5일
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.
Vincent
Vincent 2012년 11월 5일
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?
Daniel Shub
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.
Vincent
Vincent 2012년 11월 5일
Here you are.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

태그

질문:

2012년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by