How to save memory: loops or copy paste
古いコメントを表示
I am wondering how one can save precious RAM in order to be able to handle larger arrays or more complex stuff.
Which uses less memory: using a loop to repeat a code (e.g.) three times or to copy the respective code three times in a row?
採用された回答
その他の回答 (1 件)
Walter Roberson
2015 年 5 月 13 日
0 投票
To reduce memory usage, pre-allocate the output arrays to avoid growing them as you go.
Code repeated multiple times will take more space in the MATLAB interpreter than a loop would take, unless perhaps the code was nearly trivial. However, repeating code multiple times can be more efficient than looping.
You should consider using single precision instead of double precision. If you do that, make sure that any constants you use in the code do not accidentally change the precision of the calculation.
But mostly... get more RAM and a 64 bit version of MATLAB. Memory is too inexpensive these days to make it worth the time it takes to use the minimum amount of memory -- not unless you are creating code to go into a limited-resource embedded system.
カテゴリ
ヘルプ センター および 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!