Saving class objects in array suddenly tanking performance

조회 수: 6 (최근 30일)
William
William 2024년 8월 2일
댓글: Kevin Holly 2024년 8월 9일
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.
  댓글 수: 6
William
William 2024년 8월 2일
Also just wanna mention I edited the post with slightly more details on the performance impact, and that I tried swapping these value classes to handles and that has made the performance fine, but feels counterintuitive to me as I expected handles to have more overhead, not less (AFAIK this was historically the case with Matlab).
Kevin Holly
Kevin Holly 2024년 8월 9일
While handle classes might have some inherent overhead due to memory management, the benefits of avoiding deep copies often outweigh these costs, especially for large or complex objects. In your case, the significant performance improvement after switching to handle classes suggests that the overhead of copying value class objects was the primary bottleneck.
MATLAB's handling of structs vs. classes (especially value classes) can lead to significant performance differences due to copying and memory management overhead. For simple data holders, using structs is often more efficient. If you need the features of classes, handle classes can help mitigate copying overhead.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by