Consider preallocating for speed [duplicate]

Solution 1:

Yes. Each time you go around, your elseif block is resizing symbol_chip, which is expensive. Instead, rewrite your code so that you have (say) symbol_chip = zeros(max_size, 1); before the loop. Then, change the contents but not the size of symbol_chip.

You'll need to slightly change your approach, but it will be much faster if you do. If you're not annoyed by the current speed, don't change anything!

Solution 2:

M-Lint will throw this warning if you have a variable that grows inside a loop without being preallocated. You can remove this error by pre-allocating the collection variable.

For instance, if you knew that the variable symbol_chip would have at most i*j elements, you could preallocate it with the statement:

symbol_chip = zeros(i*j);

However, for most applications preallocation will only have a negligible effect on algorithm performance. I would only worry about it if you are dealing with very large data sets.