MATLAB Answers

performance when copying data from object array to cell

1 view (last 30 days)
Darim
Darim on 9 Jan 2017
Edited: Darim on 12 Jan 2017
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

Sign in to comment.

Accepted Answer

Kirby Fears
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

Show 5 older comments
Darim
Darim on 10 Jan 2017
Timing is the same as for
data = {cls.data}
i.e.
% result:
% N = 1000: T = 0.003s
% N = 10000: T = 0.32s
% N = 20000: T = 1.11s
% N = 30000: T = 2.44s
Which makes sense since the dynamic property access has to be resolved only once.
In addition, I've quickly installed 2016b an can confirm your numbers:
%%Adam solution (use 2016b)
tic;
data = { cls.( pName ) };
toc;
% result:
% N = 1000: T = 0.0009s
% N = 10000: T = 0.007s
% N = 20000: T = 0.014s
% N = 30000: T = 0.018s
So, from this perspective, the neat solution looks great.
Thanks, Daniel
Kirby Fears
Kirby Fears on 10 Jan 2017
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.
Darim
Darim on 12 Jan 2017
Kirby, Adam,
since you were so supportive regarding my problem, I'd like to share the information I've received from Matlab support.
[Quote:] "The test case creates N number of objects with data that are assigned to exactly the same array. This causes MATLAB to create a long list of arrays sharing the same data to avoid making data copies. However, when creating the cell array, MATLAB ends up traversing this long list for each element. An optimization to handle this case better was introduced in 2016a.
In a real-world scenario, would thousands of the objects really still have the default value? You can see that the timing is linear as expected when using random data in each object instance:"
They enhanced the class:
%File: TestClass1.m
classdef TestClass1
%CLASS Summary of this class goes here
% Detailed explanation goes here
properties
data %= [1 2 3 4 5];
end
methods
function obj = TestClass1
obj.data = rand(1,5);
end
end
end
They tested:
%File: runTestClass1.m
function runTestClass1
runOneTest(1000);
runOneTest(2000);
runOneTest(10000);
end
function runOneTest(N)
% create N objects
for k = N:-1:1
cls(k) = TestClass1;
end
% collect data
tic;
data = {cls.data};
toc;
end
And they received:
>> runTestClass1
Elapsed time is 0.000493 seconds.
Elapsed time is 0.000863 seconds.
Elapsed time is 0.004480 seconds.
As well I did: I can confirm.
Cool!
Thanks again, Daniel

Sign in to comment.

More Answers (0)

Products