Array of objects - speed issues

조회 수: 5 (최근 30일)
Gabriel Caron L'Écuyer
Gabriel Caron L'Écuyer 2012년 3월 3일
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

답변 (2개)

Laurens Bakker
Laurens Bakker 2012년 3월 7일
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
Laurens Bakker
Laurens Bakker 2012년 3월 7일
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 2012년 3월 7일
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.

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


Daniel Shub
Daniel Shub 2012년 3월 7일
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:

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by