Performance when creating an array of class objects

17 vues (au cours des 30 derniers jours)
Marty Stevens
Marty Stevens le 29 Août 2014
Réponse apportée : Adam le 29 Août 2014
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

Réponses (1)

Adam
Adam le 29 Août 2014
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.

Catégories

En savoir plus sur Construct and Work with Object Arrays dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by