Numerical operations are slow on class properties versus in workspace
Show older comments
Hello,
I ran into this behavior I don't really understand: simple numerical operations are order 10x+ slower on class properties than when performed in the workspace.
As an example, I will create a circular index that I want to increment by 1.
N = 2^16; % Number of times to increment
M = 2^8; % What is our wrap point
I = 0; % Initialize our index
t = tic;
clc
disp("Incrementing in workspace");
for n = 1:N
I = mod(I+1,M); % Increment, with wrapping
end
fprintf("%2.0f kOps/second\n",N/toc(t)/1000); % 1000s of operations/second
Great, I am getting ~ 45,000 kOps/second.
Now, instead I will do this all in a class:
classdef speedTestClass < handle
properties
I = 0;
M
end
methods
function increment(obj)
obj.I = mod(obj.I + 1,obj.M); % Increment, with wrapping
end
end
end
And then run it
ob = speedTestClass; % Make the object
ob.M = M; % Set the wrap point
disp("Incrementing in class");
t = tic;
for n = 1:N
ob.increment % Increment, with wrapping
end
fprintf("%2.0f kOps/second\n",N/toc(t)/1000);
Where I get 2400 kOps/second. A slowdown factor of 20x. Yikes.
So, is there a way to more efficiently perform simple operations on class properties? Is there some MEX magic working behind the scenes, and so I would need to MEX my classes?
I couldn't think of a simpler example than this basic index container; I could understand a factor of 2x speed loss, but 20x is huge, and it only gets worse when the computation is more involved and involves multiple methods/properties of the class.
I'll note that on a lark, I actually compiled this into an exe and ran it - the workspace version dropped to the speed of the class based one! Oh no.
Cheers all,
-Dan
4 Comments
James Tursa
on 22 Jun 2020
Edited: James Tursa
on 22 Jun 2020
Regarding mex, the problem is that there are NO official API functions to get at the data pointers of classdef variable properties. So anything you do with the properties in a mex routine will involve a DEEP data copy for both getting the data and setting the data ... that's two deep data copies of the entire data set. This could be a major drag to any performance gains you would get by using compiled code.
Matt J
on 23 Jun 2020
Edited: per isakson
on 26 Jun 2020
simple numerical operations are order 10x+ slower on class properties than when performed in the workspace.
The bottlenecks that you see are not indemic only to property accesses as the modified versions of your tests below will show (in R2019a). They appear to occur in method calls as well, with particularly bad performance for staitc methods.
classdef myclass < handle
properties
I = 0;
M
end
methods
function I=increment(obj,I,M)
I = mod(I + 1,M); % Increment, with wrapping
end
end
methods (Static)
function I=static_increment(I,M)
I = mod(I + 1,M); % Increment, with wrapping
end
end
end
t = tic;
for n = 1:N
I = mod(I+1,M); % Increment, with wrapping
end
fprintf("%2.0f kOps/second\n",N/toc(t)/1000); % 1000s of operations/second
ob = myclass; % Make the object
disp("Incrementing in method call");
t = tic;
for n = 1:N
I=ob.increment(I,M); % Increment, with wrapping
end
fprintf("%2.0f kOps/second\n",N/toc(t)/1000);
disp("Incrementing in property access");
ob.I=I; ob.M=M;
t = tic;
for n = 1:N
ob.I = mod(ob.I+1,ob.M); % Increment, with wrapping
end
fprintf("%2.0f kOps/second\n",N/toc(t)/1000);
disp("Incrementing in static method call");
t = tic;
for n = 1:N
I = myclass.static_increment(I+1,M); % Increment, with wrapping
end
fprintf("%2.0f kOps/second\n",N/toc(t)/1000);
Incrementing in workspace
41935 kOps/second
Incrementing in method call
1746 kOps/second
Incrementing in property access
2758 kOps/second
Incrementing in static method call
8 kOps/second
per isakson
on 23 Jun 2020
Daniel Plotnick
on 23 Jun 2020
Answers (1)
It is the repeated M-coded function calls that are slowing you down. Implement the whole loop in a single function call:
function increment(obj,N)
M=obj.M;
I=obj.I;
for n = 1:N
I = mod(I+1,M); % Increment, with wrapping
end
obj.I=I;
end
4 Comments
Daniel Plotnick
on 22 Jun 2020
I understand that your original example was a simplified version, but it doesn't change what the problem is nor the solution that you will need to pursue.You need to re-organize your code so that your loop iterations contain a higher propertion of vectorized commands and a lower proportion of method calls and property accesses. Otherwise, the overhead for the latter will be a bottleneck.
Daniel Plotnick
on 23 Jun 2020
Edited: per isakson
on 26 Jun 2020
See my updated analysis above,
I now think you are seeing overhead from classdef's specifically, though not just from property access. I think the bottom line is one just needs to try to put loops inside functions and not the other way around. and/or to write loops that do lots of computation per iteration (esp. with vectorized commands) instead of really small and quick tasks. These were always best practices in writing efficient Matlab code, although I have to admit, I thought TMW had mananged to better optimize class operations over the years than what we're seeing now.
Categories
Find more on Construct and Work with Object Arrays 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!