Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Inspired by the answers of <a href="https://stackoverflow.com/users/669202/jonas-heidelberg">@Jonas Heidelberg</a> and <a href="https://stackoverflow.com/users/669202/jonas-heidelberg">@Tobias Heß</a> I came up with the following <strong>solution that does not need cell arrays</strong> at all. The objects are stored in an auxiliary array and once this is completed they are copied to the property array:</p> <p><strong>Code for HostingObject with array "post-allocation":</strong></p> <pre><code>classdef JacobHostingObject &lt; handle % This Objects Hosts a List of Objects that are created in the constructor properties ObjectList=SomeSimpleObject end methods 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" end end end </code></pre> <p><strong>So how do the three solutions (Jonas, Tobias and mine) perform?</strong></p> <p>I ran some tests (with MATLAB R2011b 64bit) to compare them:</p> <p>10.000 Objects</p> <p>Elapsed Time: <strong>Tobias:0,30 sec</strong>; Jacob:0,31 sec; Jonas:2,02 sec; Original:56,79 sec</p> <p>100.000 Objects</p> <p>Elapsed Time: <strong>Tobias:2,42 sec</strong>; Jacob:2,95 sec; Jonas:203,03 sec</p> <p>1.000.000 Objects</p> <p>Elapsed Time: <strong>Tobias:23,84 sec</strong>; Jacob:29,18 sec</p> <p><strong>So it looks like Tobias version is the fastest solution</strong></p> <p>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 <a href="https://stackoverflow.com/questions/7676162/slow-performance-when-storing-handle-objects-in-a-cell-array">"slow performance when storing handle objects in a cell array"</a>. 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).</p> <p><strong>However, it is still not clear what is actually going on inside MATLAB that causes the differences in performance</strong>. It might be helpful to get an understanding of this to avoid running into similar problems in the future!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload