Array of objects - speed issues

4 visualizaciones (últimos 30 días)
Gabriel Caron L'Écuyer
Gabriel Caron L'Écuyer el 3 de Mzo. de 2012
I am programming a simulation that would be an big, huge, enormous pain not to code in an object-oriented way (complex objects interrelated, myclass<handle). The problem I am facing is that when using arrays of objects (either with a cell or vector architecture) matlab gets very slow (in an almost-incomprehensible way) when the arrays gets bigger.
For reference : time to add an object...
-for an array of a few objects : fast
-for an array of 20 objects : 1 sec
-for an array of 100 objects : way too slow
The problem is not the hardware.
I have two "kind" of objects:
-complex objects that I don't need to be lighning fast, but I would need the time to add one object to an array of 100 objects to be below 0.1 sec.
-simple objects that are quite numerous (arrays of 5000 to 10000) and for which the processing speed is important. If there is no solution, those can be implemented in a non-object oriented way.
Does anyone have faced this problem, does anyone have some hints for me. Thanks. Gabriel

Respuestas (2)

Laurens Bakker
Laurens Bakker el 7 de Mzo. de 2012
Hi Gabriel,
I have had the same experience of MATLAB slowing down when using OO design, particularly with handles. I've never tried to re-size arrays on the fly, and it is good MATLAB practice to pre-allocate those arrays. If you know that you will never need more than N objects, you could try something like:
repmat( obj, [1 N] );
Cheers,
Laurens
  2 comentarios
Laurens Bakker
Laurens Bakker el 7 de Mzo. de 2012
The reason why the resizing is slow, is that MATLAB actually creates a whole new array of objects, and then needs to deep copy all the old objects over to this new array, add the new one, and then clean up the old array.
Daniel Shub
Daniel Shub el 7 de Mzo. de 2012
I am not sure MATLAB memory management works in this way for arrays of nonstandard data types (e.g., cells and user defined classes). I think each object is stored separately and not influenced by modifying the array; in other words the array is just a bunch of pointers to the objects. I think this has to be true of handle classes.

Iniciar sesión para comentar.


Daniel Shub
Daniel Shub el 7 de Mzo. de 2012
This is a well known problem with the OO system in MATLAB and unfortunately there is no simple solution.
See a comment to an answer by Matt Fig by owr in this question:

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by