How to avoid Matlab taking exponential time when creating a handle object array as an object property How to avoid Matlab taking exponential time when creating a handle object array as an object property arrays arrays

How to avoid Matlab taking exponential time when creating a handle object array as an object property


I don't know why your code is slow, but I found a way to fix it: use a cell array instead of a normal array. I don't find it completely intuitive myself, but here are my profiling results:

>> tic,a=HostingObject;tocElapsed time is 105.320128 seconds.>> tic,a=JonasHostingObject;tocElapsed time is 2.394570 seconds.

The object is defined as follows:

classdef JonasHostingObject < handleproperties    ObjectListendmethods    function obj=JonasHostingObject()            obj.ObjectList=cell(10000,1);        for n=1:10000            obj.ObjectList{n}=SomeSimpleObject();        end    endend    end

I'm using MATLAB R2011a 64bit, in case it matters.


Inspired by the answers of @Jonas Heidelberg and @Tobias Heß I came up with the following solution that does not need cell arrays at all. The objects are stored in an auxiliary array and once this is completed they are copied to the property array:

Code for HostingObject with array "post-allocation":

classdef JacobHostingObject < handle% This Objects Hosts a List of Objects that are created in the constructorproperties    ObjectList=SomeSimpleObjectendmethods    function obj=JacobHostingObject()                 for n=10000:-1:1 % counting backwards for Memory pre-allocation                     a(n)=SomeSimpleObject(); % auxiliary array for "post-allocation"                   end                     obj.ObjectList=a; % "post-allocation"    endendend

So how do the three solutions (Jonas, Tobias and mine) perform?

I ran some tests (with MATLAB R2011b 64bit) to compare them:

10.000 Objects

Elapsed Time: Tobias:0,30 sec; Jacob:0,31 sec; Jonas:2,02 sec; Original:56,79 sec

100.000 Objects

Elapsed Time: Tobias:2,42 sec; Jacob:2,95 sec; Jonas:203,03 sec

1.000.000 Objects

Elapsed Time: Tobias:23,84 sec; Jacob:29,18 sec

So it looks like Tobias version is the fastest solution

It is interesting to note, that Jonas solution is much better than the original but scales much worse to the other two solutions. This confirms the observation that I made in my earlier question "slow performance when storing handle objects in a cell array". It is a bit ironic that using the technique that was cause of my earlier problem turns out to be an improvement to this problem. Yet, Tobias solution even answers my old question as well (I will post a reference there).

However, it is still not clear what is actually going on inside MATLAB that causes the differences in performance. It might be helpful to get an understanding of this to avoid running into similar problems in the future!


I found an onther answer. I think using a cell array is not a good solution because you have not the same accsses to the objects as in an array. But you can use a cell as workaround for the problem:

classdef HostingObject < handle% This Objects Hosts a List of Objects that are created in the% constructorproperties    ObjectList=SomeSimpleObjectendmethods    function obj=HostingObject()        % Creating first a cell array        helpCell = cell(10000,1);        for n=1:10000             helpCell{n}=SomeSimpleObject();        end        % Convert the cell array to the object array        obj.ObjectList = horzcat(helpCell{:});    endendend