How do I improve the performance of copying and assigning arrays of Classdef object?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hello!
I have the following classdef class that implements a custom copy()-like method, deepCopy(). I have shown the class, truncated, below:
classdef LaunchVehicleStageState < matlab.mixin.SetGet & matlab.mixin.Copyable
properties
stage(1,:) LaunchVehicleStage
active(1,1) logical = true;
engineStates(1,:) LaunchVehicleEngineState
tankStates(1,:) LaunchVehicleTankState
end
methods
function obj = LaunchVehicleStageState(stage)
if(nargin > 0)
obj.stage = stage;
end
end
%... truncated here ...
function newStageState = deepCopy(obj)
newStageState = obj.copy();
% newStageState.active = obj.active; %don't think I need to
% copy this beacuse it's a logical and should just come with it
newEngineStates = obj.engineStates.copy();
[newEngineStates.stageState] = deal(newStageState);
newStageState.engineStates = newEngineStates;
newTankStates = obj.tankStates.copy();
[newTankStates.stageState] = deal(newStageState);
newStageState.tankStates = newTankStates;
end
end
end
The goal of the deepCopy() method is to copy the instance of LaunchVehicleStageState, copying also the engineStates and tankStates associated with it. Basically I need a selective deep copy of the LaunchVehicleStageState.
However, deepCopy() is slow. In fact, when profiling, it is the single largest "Self-time" user by a factor of 2. The time is more or less evenly distributed among those last 6 lines of code in deepCopy(). Say the total self-time in that deepCopy() method is 1 second. Then about 0.5 seconds is used in the "engine states" copy section and 0.5 seconds is used in the "tank states" copy section. I would like to try and improve the performance of deepCopy() so that it runs quicker.
I don't believe there's much I can do about the two (or three, really) copy() calls in deepCopy(). Those are internal methods to MATLAB and I probably can't speed them up. The copy() methods also only use about 0.1 seconds total (across 3200 total calls), though, and so are the smaller of the targets to optimize.
However, I am hoping that there might be a more efficient way of assigning the "stateState" property of the "engineStates" and the "tankStates", and then assigning those object arrays back to their respective properties in "newStageState". Togther, these four calls (two calls to deal() and two newStageState.set() calls). Each of those four calls uses abot 0.2 seconds of time total (across 3200 total calls).
Any thoughts? Is there some way I can cheapen the cost of setting the properties of the newStageState object (the last of three lines in both blocks)? What about the deal calls, is there a more efficient way of handling that? Is there something else I can do to make all this run a bit faster? The whole code only takes about 4 seconds to run, so spending 25% of that time copying objects seems a bit probablematic and I'm hopeful there's something I can do about it.
Thank you!
0 comentarios
Respuestas (0)
Ver también
Categorías
Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!