struct field access slower with R2015b than with R2013a

1 view (last 30 days)
I am looking at the speed of operations on object fields using methods vs. direct object field access vs. struct field access vs. operation on plain numeric matrices, and the differences between R2015b and R2013a. Take the simple class
classdef myclass
properties
v = [1:10].';
end
methods
function obj = inc(obj)
obj.v = obj.v+1;
end
end
end
and a test script
disp('o = inc(o)');
o = myclass();
tic;
for k = 1:100000,
o = inc(o);
end
toc;
disp('o = o.inc()');
o = myclass();
tic;
for k = 1:100000,
o = o.inc();
end
toc;
disp('o.v = o.v+1');
o = myclass();
tic;
for k = 1:100000,
o.v = o.v+1;
end
toc;
disp('s.v = s.v+1');
o = myclass();
s = struct(o);
tic;
for k = 1:100000,
s.v = s.v+1;
end
toc;
disp('v = v+1');
o = myclass();
v = s.v;
tic;
for k = 1:100000,
v = v+1;
end
toc;
that outputs
o = inc(o)
Elapsed time is 2.181198 seconds.
o = o.inc()
Elapsed time is 2.719772 seconds.
o.v = o.v+1
Elapsed time is 0.135281 seconds.
s.v = s.v+1
Elapsed time is 0.020724 seconds.
v = v+1
Elapsed time is 0.010462 seconds.
on R2013a and
o = inc(o)
Elapsed time is 0.650212 seconds.
o = o.inc()
Elapsed time is 0.686497 seconds.
o.v = o.v+1
Elapsed time is 0.090132 seconds.
s.v = s.v+1
Elapsed time is 0.093253 seconds.
v = v+1
Elapsed time is 0.009202 seconds.
on R2015b on the same machine. Obviously, method invocation has become a lot faster between releases, the direct access of object properties is now a little faster. Operating on matrix v directly has improved marginally. What puzzles me, however, is that operating on a field of struct s almost takes 5 times as long in R2015b as in R2013a. Does anyone know why this is the case, and whether there is a workaround to this behavior?
  3 Comments
Marc Passy
Marc Passy on 28 Jan 2016
I've noticed much the same. Here, dts is an array of datenums, that I'm testing for a valid range. Test one uses a local variable, and test two uses a CONSTANT from a locally-defined class.
MIN_DATE = datenum('1 Jan 1910');
MAX_DATE = datenum('31 Dec 2150');
tic
for i = 1:10000;
(a >= MIN_DATE) & (a <= MAX_DATE);
end
toc
tic
for i = 1:10000;
(a >= DateUtil.MIN_DATE) & (a <= DateUtil.MAX_DATE);
end
toc
And the results:
Elapsed time is 0.014219 seconds.
Elapsed time is 3.567381 seconds.
Anyone have any ideas why constant access is so slow? There is no get function either - it's just a variable.
Walter Roberson
Walter Roberson on 28 Jan 2016
The engine was redesigned in time for R2015b, and in particular method access was made faster. However, I have no information about changes to struct.

Sign in to comment.

Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!