If the memory copy occurs when execute an assignment or build a temporary object?
3 views (last 30 days)
Show older comments
For example,
A =zeros(10, 10) %is a matrix.
B = A; % statement 1
C = A(:,6:end); % statement 2
D = C *C'; % statement 3
E = A(:,6:end)*A(:,6:end)'; %statement 4
1)
I wonder if the statement 1 copys the memory of A to B?
I learned from the textbook about the function call mechanism of MATLAB. Whether an argument will be copied depends on whether the value of this argument is changed inside the function. I want to know if this mechanism is consistent in MATLAB.
If the above mechanism is consistent in MATLAB, my understanding is whether memory will be copied should be judged according to the context. If B does not change in the subsequent program, it should only be a reference of A without copying memory. Copy will occur only when B changes the matrix content. Is is correct?
2)
If the statement 2 copys memory?
3)
Like the above question, I want to know whether the construction of A(:,6:end) and A(:,6:end)' temporary objects will copy memory, and whether statement 3 and statement 4 are equivalent in memory consumption and running speed?
Many thanks!
0 Comments
Answers (1)
Walter Roberson
on 7 Oct 2021
I wonder if the statement 1 copys the memory of A to B
No. B will be assigned as a reference to the same memory as A.
If the statement 2 copys memory?
Traditionally, the right hand side would have copied memory. A few releases ago, MATLAB added an internal mechanism for referring to sub-portions of an array without copying the memory, but it is difficult to find documentation of that mechanism. I believe that in the context of an assignment the mechanism would ultimately cause memory to be copied -- that is, I believe that the reference mechanism is read-only for temporary reference to values that are going to be used in calculations. But documentation is scarce on that point.
| want to know whether the construction of A(:,6:end) and A(:,6:end)' temporary objects will copy memory
We are not certain. The model is that each of the two A(:,6:end) would have copied memory independently -- but it was always plausible that as an optimization, MATLAB might have noticed that A(:,6:end) and the following A(:,6:end) referred to the same place, so it is possible that it would only have copied the memory once instead of twice. However, that possibility feels unlikely, since A(:,6:end)*A(:,6:end)' is properly parsed as A(:,6:end)*(A(:,6:end)') and the transpose operation needs to copy memory -- but we cannot rule out the possibility that it optimizes down to a single copy of A(:,6:end) .
and whether statement 3 and statement 4 are equivalent in memory consumption and running speed
Oddly there are special internal rules for all-zero arrays that complicate the situation. It would be better to test with non-zero arrays.
N = 8e3;
A = rand(N, N);
tic;
C = A(:,6:end);
D = C*C';
toc
tic
E = A(:,6:end) * A(:,6:end)';
toc
tic;
C = A(:,6:end);
D = C*C';
toc
tic
E = A(:,6:end) * A(:,6:end)';
toc
I think we have to conclude that the temporary variable form uses less time.
(I run each of the calculations twice in case some kind of optimization occurs after the first time.)
See Also
Categories
Find more on Deploy to C++ Applications Using mwArray API (C++03) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!