How to save memory: loops or copy paste
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 13 de Mayo de 2015
0 votos
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.
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!