Performance when creating an array of class objects
17 次查看(过去 30 天)
显示 更早的评论
Hi
Using Matlab 2010b Service Pack 1. We are having a performance issue when creating an array of 1000 objects.
The array is obj.MyClassArray
The objects are of class
classdef MyClass < handle
properties
prop1
prop2
prop3
nameValueContainerMap % a container.Map(name, MySeriesObject)
end
The map class is
classdef MySeries < handle
properties
dates
values % a cell containing n doubles
end
The cell consists of an array of doubles.
We are pre-allocating memory by initialising obj.MyClassArray with 1000 copies of a fully populated object of MyClass, using repmat.
However, we are still finding a performance issue.
Any ideas?
Thanks
Marty
0 个评论
回答(1 个)
Adam
2014-8-29
If your class has a default constructor (or basically your constructor can handle the 0-argument case) then the simplest way to pre-allocate an array of 1000 objects is just to type:
myArray(1,1000) = MyClass;
You say "1000 copies of a fully populated object" though which suggests it isn't infact pre-allocation you are doing, but full array allocation already initialised objects. In that case you can't do what I suggested.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!