performance when copying data from object array to cell
4 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Kirby Fears
on 9 Jan 2017
Edited: Kirby Fears
on 9 Jan 2017
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 Comments
More Answers (0)
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!