Saving class objects in array suddenly tanking performance
So, I have this tool I’ve developed to do some radar simulations. One small part of it is I use this class just for bundling and passing around detection data instead of a struct:
classdef Detection
% Data holder for a radar detection
properties
time (1,1) double
snr (1,1) double
raer (1,4) double
sigmas (1,4) double
end
methods
function obj = Detection(time,snr,raer,sigmas)
if nargin > 0 % Allow no args constructor to be called to make empty detections
obj.time = time;
obj.snr = snr;
obj.raer = raer;
obj.sigmas = sigmas;
end
end
end
end
These get created in one handle class then passed off to another that saves data on a given target. In that object, I save them into a (pre-allocated* array):
this.detections(i) = det;
After making some code changes yesterday, suddenly this one line went from being trivial computationally to 70% of the run-time of a basic sim run when profiled, more than tripling the run time of said sim from ~1s to ~3s for 761 detections saved. I can’t figure out how to pull out any more details about what’s suddenly making a simple operation like that so slow, or how to work around it. I’ve run into some weird behavior like this in the past when saving simple data holder objects caused bizarre performance issues, but usually I could tweak something or find a work around, here I’m just stumped as to what caused it because I didn’t even change anything about the object or how they’re saved.
*Specifically, they start with a base size and then if the sim runs long enough to outstrip that I start doubling the data arrays, just thought I’d add that in case there’s some weird potential behavior there.
Edit: I tried making this class and the Dwell class (other one mentioned in comments) handles, and that seems to have alleviated the problem (although still seems noticeably slower than saving structs), but I’m curious as to why saving value classes is seemingly so slow.So, I have this tool I’ve developed to do some radar simulations. One small part of it is I use this class just for bundling and passing around detection data instead of a struct:
classdef Detection
% Data holder for a radar detection
properties
time (1,1) double
snr (1,1) double
raer (1,4) double
sigmas (1,4) double
end
methods
function obj = Detection(time,snr,raer,sigmas)
if nargin > 0 % Allow no args constructor to be called to make empty detections
obj.time = time;
obj.snr = snr;
obj.raer = raer;
obj.sigmas = sigmas;
end
end
end
end
These get created in one handle class then passed off to another that saves data on a given target. In that object, I save them into a (pre-allocated* array):
this.detections(i) = det;
After making some code changes yesterday, suddenly this one line went from being trivial computationally to 70% of the run-time of a basic sim run when profiled, more than tripling the run time of said sim from ~1s to ~3s for 761 detections saved. I can’t figure out how to pull out any more details about what’s suddenly making a simple operation like that so slow, or how to work around it. I’ve run into some weird behavior like this in the past when saving simple data holder objects caused bizarre performance issues, but usually I could tweak something or find a work around, here I’m just stumped as to what caused it because I didn’t even change anything about the object or how they’re saved.
*Specifically, they start with a base size and then if the sim runs long enough to outstrip that I start doubling the data arrays, just thought I’d add that in case there’s some weird potential behavior there.
Edit: I tried making this class and the Dwell class (other one mentioned in comments) handles, and that seems to have alleviated the problem (although still seems noticeably slower than saving structs), but I’m curious as to why saving value classes is seemingly so slow. So, I have this tool I’ve developed to do some radar simulations. One small part of it is I use this class just for bundling and passing around detection data instead of a struct:
classdef Detection
% Data holder for a radar detection
properties
time (1,1) double
snr (1,1) double
raer (1,4) double
sigmas (1,4) double
end
methods
function obj = Detection(time,snr,raer,sigmas)
if nargin > 0 % Allow no args constructor to be called to make empty detections
obj.time = time;
obj.snr = snr;
obj.raer = raer;
obj.sigmas = sigmas;
end
end
end
end
These get created in one handle class then passed off to another that saves data on a given target. In that object, I save them into a (pre-allocated* array):
this.detections(i) = det;
After making some code changes yesterday, suddenly this one line went from being trivial computationally to 70% of the run-time of a basic sim run when profiled, more than tripling the run time of said sim from ~1s to ~3s for 761 detections saved. I can’t figure out how to pull out any more details about what’s suddenly making a simple operation like that so slow, or how to work around it. I’ve run into some weird behavior like this in the past when saving simple data holder objects caused bizarre performance issues, but usually I could tweak something or find a work around, here I’m just stumped as to what caused it because I didn’t even change anything about the object or how they’re saved.
*Specifically, they start with a base size and then if the sim runs long enough to outstrip that I start doubling the data arrays, just thought I’d add that in case there’s some weird potential behavior there.
Edit: I tried making this class and the Dwell class (other one mentioned in comments) handles, and that seems to have alleviated the problem (although still seems noticeably slower than saving structs), but I’m curious as to why saving value classes is seemingly so slow. class, objects, performance, arrays MATLAB Answers — New Questions