MATLAB Answers

Cell-Preallocation slows down code

4 views (last 30 days)
Vincent
Vincent on 5 Nov 2012
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
Vincent
Vincent on 5 Nov 2012
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.

Sign in to comment.

Accepted Answer

Daniel Shub
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

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!