optimize the end statement in a for loop

1 visualización (últimos 30 días)
Victor
Victor el 24 de En. de 2014
Respondida: Walter Roberson el 24 de En. de 2014
I have a loop that runs millions of times that cannot be arrayed. Using the profiler I've noticed some of the lines that take up the most time are the end statements. Are there any ways to make the end statement of the for loop more efficient such as preallocation of single variables or clearing variables before the end of a loop?
  1 comentario
ES
ES el 24 de En. de 2014
If in the loops you use, you keep changing the dimension of arrays (by append operation or so), then pre-allocation saves time.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de En. de 2014
The "end" statement itself does not take any time, but when MATLAB needs some time to do internal operations for the loop, the time is sometimes marked as being on the "end" because it is not really directly associated with the other lines.
You should, in particular, pre-allocate any arrays that you write inside of. However, arrays that you overwrite completely without any subscripts, do not need to be preallocated.
clearing variables before the end of the loop is, in most cases, not necessary and could slow you down. There are some cases where it helps.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by