performance when copying data from object array to cell
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, I encounter performance issues, when copying data from an object property to a cell. My test class looks as follows:
classdef Class < handle
%CLASS Summary of this class goes here
% Detailed explanation goes here
properties
data = [1 2 3 4 5];
end
methods
end
end
My test script is the following:
% settings
N = 20000; % try 1000/10000
% create N objects
cls(N) = Class();
% collect data
tic;
data = {cls.data};
toc;
% result:
% N = 1000: T = 0.008s
% N = 10000: T = 0.6s
% N = 20000: T = 2.4s
I'd expect linear scaling of computation time with array size. This however does not hold. Can someone give a hint about how to increase copy performance in this example? Is there a reason why it does not scale linearly?
Thanks, Daniel
댓글 수: 0
채택된 답변
Kirby Fears
2017년 1월 9일
편집: Kirby Fears
2017년 1월 9일
Darim,
Since you are not pre-allocating the data cell, Matlab is probably expanding the size of data iteratively. With pre-allocation the timing is linear. Try for yourself.
% settings
N = 20000; % try 1000/10000
% create N objects
cls(N) = Class();
% collect data
tic;
data = cell(1,N);
for i = 1:numel(cls),
data{i} = cls(i).data;
end
toc;
댓글 수: 8
Kirby Fears
2017년 1월 10일
편집: Kirby Fears
2017년 1월 10일
Adam's approach is definitely best for the latest Matlab release. As for 2015b, I don't see a direct way to dynamically access a single property from the class without suffering the string resolution time.
If you know all the properties you might want to extract from your class, and if the contents of those properties are not too large, you could extract all properties during the loop (using hard coded names) into a MxN cell array with corresponding collection of property name strings like {'prop1','prop2',...,'propN'}.
After the loop, you can extract a specific property from the MxN cell array as needed. The performance of this approach relies entirely on the class you're working with. It might be faster than dynamic property access in your case.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Java from MATLAB에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!