For-loops are now faster than some of the simplest vectorized statements. Why do we still need so much deep copying?

7 views (last 30 days)
It is disconcerting to me that the for-loop implementation of operations like x(i+1)*x(i) are now faster than the vectorized implementation (see below). One of them main purposes of Matlab is to make it possible to do such operations optimally in brief, vectorized syntax.
Moreover, this problem is entirely because subsref operations like x(2:end) create a deep copy of the data. Can't the parser determine that this is unnecessary in situations where the indexed variable only appears on the right-hand side of the assignment? And if so, isn't it high time this was implemented?
N=1e8;
x=rand(1,N);
y=zeros(1,N);
%Vectorized implementation
tic;
y(1:N-1)=x(2:N).*x(1:N-1);
toc
Elapsed time is 1.574006 seconds.
%For-loop implementation
tic;
for i=1:N-1
y(i)=x(i+1).*x(i);
end
toc
Elapsed time is 0.576191 seconds.
%Remove contribution of deep copies
X1=x(2:end); X2=x(1:end-1);
tic;
y(1:N-1)=X1.*X2;
toc
Elapsed time is 0.441418 seconds.
  1 Comment
Jan
Jan on 12 Nov 2021
Edited: Jan on 13 Nov 2021
By the way: Timings in R2009a to R2015b:
% Elapsed time is 1.998664 seconds. Vectorized with copy
% Elapsed time is 4.307474 seconds. Loop
% Elapsed time is 0.798561 seconds. Vectorized without copy
R2016b:
% Elapsed time is 1.142006 seconds.
% Elapsed time is 0.733920 seconds.
% Elapsed time is 0.633730 seconds.
R2018b:
% Elapsed time is 0.856713 seconds.
% Elapsed time is 0.500901 seconds.
% Elapsed time is 0.427571 seconds.
Win 10, i7, 16 GB RAM.
Even the vectorized version needs less than the half time only.

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 11 Nov 2021
Edited: James Tursa on 11 Nov 2021
I have read rumors in the past that TMW was tinkering with the idea of sub-array references, or some type of limited pointer capability. But nothing officially yet, of course. The entire endeavor becomes problematic in a dynamic variable scheme like MATLAB because the underlying memory can be pulled out from under you at any time. I.e., the source array can change and free the memory that the sub-array reference depends on. To keep the sub-array valid you have to save a shared copy of the source off to the side and keep track of it's connection to the sub-arrays. Only when all sub-arrays connected to the source array are released can you finally free the background shared source array you saved off to the side. It's a bookkeeping headache, but that is in fact what I do with my SHAREDCHILD mex submission:
The problem with this submission, however, is TMW keeps changing the underlying mxArray header to thwart my efforrts. It used to be that the linked list of all shared data copies was visible in the header. Not any more. So SHAREDCHILD is now broken for the latest versions of MATLAB, and without that linked list I don't know if I can repair it. Maybe I can get by with just the counter that is now visible, but I haven't had the time to work on this yet.
"Isn't it high time this was implemented?"
My obious answer is yes, of course. These temporary deep copies of large variables have long been a source of memory/timing issues in MATLAB.
  15 Comments
Matt J
Matt J on 19 Nov 2021
I have read rumors in the past that TMW was tinkering with the idea of sub-array references, or some type of limited pointer capability. But nothing officially yet, of course.
Maybe this counts as something official? From tech-support:
Sorry for keeping you waiting. Here's the updates from our investigation: Our development team is aware of the potential for this optimization and have it on our roadmap and we will be working on it to make it available in the future.
Thank you for bringing this issue to our attention, we will use this example as an input for our design. I am going to mark this case as closed now from a technical support perspective and you will be notified once the feature is implemented in future releases. If you have a new technical support question, please submit a new request here:
Sincerely,
Jijie Zhou
MathWorks Technical Support Department

Sign in to comment.

More Answers (1)

Jan
Jan on 13 Nov 2021
I have heared the moaning in the net, that Matlab processes loops very slow. Then MathWorks has introduced the JIT acceleration in R6.5 (2002) and loops are processed with a fair speed.
Between R2015b and R2016b the speed of a simple loop using double was improved massively. Nice.
Now the deep data copies are the new bottleneck. The discussion with Walter mentions some serious problems with fixing this. We see, that MathWorks has restricted the possibility to share variables between nested functions and scripts in the past. A possible explanation is, that they go in the direction you are dreaming of.
I suggest to call one of the designers and ask for the future plans. MathWorks does not publish the strategies for the future. But if you ask personally in a private phone call...

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!