For-loops are now faster than some of the simplest vectorized statements. Why do we still need so much deep copying?
7 次查看(过去 30 天)
显示 更早的评论
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
%For-loop implementation
tic;
for i=1:N-1
y(i)=x(i+1).*x(i);
end
toc
%Remove contribution of deep copies
X1=x(2:end); X2=x(1:end-1);
tic;
y(1:N-1)=X1.*X2;
toc
1 个评论
Jan
2021-11-12
编辑:Jan
2021-11-13
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.
采纳的回答
James Tursa
2021-11-11
编辑:James Tursa
2021-11-11
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 个评论
Walter Roberson
2021-11-20
I remember encountering sub-references in the code or documentation somewhere, but I do not remember where :(
更多回答(1 个)
Jan
2021-11-13
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...
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!