creating a temporary variable
15 ビュー (過去 30 日間)
古いコメントを表示
This is a quick question. Compare
A = sparse(...); B = A - C;
and
B = sparse(...) - C;
Memory-wise spoken, is there a difference? Will the second one use less memory because I'm not creating a variable?
0 件のコメント
採用された回答
Walter Roberson
2012 年 1 月 3 日
There will be very little memory difference. For a transient value, MATLAB creates everything about the storage except the symbol table entry with the name and a pointer to the descriptor.
3 件のコメント
Walter Roberson
2012 年 1 月 3 日
As far as I know, MATLAB only uses memory "in-place" when the same variable appears in a function call and in the assignment of the results from that call. There was another condition as well but I have forgotten it now.
I'm thinking it might have been Rick Rossom who listed the conditions in a comment, but it might have been someone else at MathWorks.
その他の回答 (1 件)
Nicholas
2012 年 1 月 3 日
Did you try using tic toc?
tic
A = sparse(magic(3));
B = A - C;
disp(toc)
tic
B = sparse(magic(3)) - C;
disp(toc)
In any case the result depends on matrix dimensions and matrix initialization. If you want to improve memory management you should initialize a matrix/vector first and than try to improve matrix calculation.
3 件のコメント
Nicholas
2012 年 1 月 3 日
If you are interested so deeply in memory allocation you should write your code in C and do not use matlab.
You can not see under the hood in matlab..
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!