Array of objects - speed issues
3 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(2 个)
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
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
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
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!