How to save memory: loops or copy paste
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 13 Mai 2015
0 votes
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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!