At which cell sizes does it make sense to preallocate memory

1 回表示 (過去 30 日間)
Julian
Julian 2018 年 9 月 6 日
編集済み: Stephen23 2018 年 9 月 6 日
Hi everyone,
so the code analyzer throws me this message and tells me that i should consider preallocating memory for speed. So i'm wondering at about which size of a matrice does it make sense to do so? In my case it is a rather small one, eg. only about 80 rows and 6 collumns. Is the 'effort' even worth it? Are there some kind of guidelines? Thanks
  1 件のコメント
Stephen23
Stephen23 2018 年 9 月 6 日
編集済み: Stephen23 2018 年 9 月 6 日
"So i'm wondering at about which size of a matrice does it make sense to do so?"
Always preallocate, if it is possible.
Preallocating even a small array will not be a disadvantage, and gives several advantages:
  • the code is automatically expandable, without needing to be rewritten.
  • the code intent is clear, making debugging and fault finding easier.
  • gets you into the habit of preallocating, so that you never have to go back through and "fix" everything later. Bad habits are much harder to unlearn than good ones are to learn.
  • makes it easy to handle mlint warnings (i.e. there should be none remaining).
I really don't see any reason to not preallocate, nor have you given any.
The whole point of best practice is that by ensuring all parts of code follow some rules and standards, then inefficiencies (programing, runtime, maintenance) and bugs are easier to avoid. But what you are suggesting is the opposite of that: a magical number that would probably change depending on the hardware and MATLAB version and who knows what else, and which becomes counter-productive as soon as you or your user applies your function to a ten million element array (which you may not have expected, but it will happen). Instead of saving time, you actually increase the time required to run and maintain the code: at some point someone will have to trawl through the functions, figure out why it is so slow, update your file versions, distribute, etc.... Yet simply writing the code following best practice right from the start would mean that never needs to happen.
I don't see any advantage to avoiding this: the "effort" typing those few characters now is much less than the real effort later trying to patch up slow code.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 9 月 6 日
From a speed perspective, it all depends on what is going on in your loop. If it takes you an hour to generate a row of the matrix, it doesn't matter if you waste 5 milliseconds to reallocate the matrix.
Theoretically, as soon as you have more than one iteration, you're wasting time since you are forcing matlab to a) create a bigger new matrix, b) copy the old matrix in the new one, c) delete the old matrix. You'll be doing that each iteration, so the more iterations, the more you'll be wasting. But for an arbitrary small number of iterations it won't be noticable. That number of iterations will depend on what goes on in the loop, as said, the speed of your processor and probably some other things.
Is the 'effort' even worth it?
It's puzzling that you see that as an effort. To me, not pre-allocating is more effort and more worries. What if the variable already exist before the loop but is bigger than needed? you'll end up with extra values in your matrix from the previous run. What if on the 10000th iteration you run out of memory adding an extra row? You'll have wasted 9999 iterations to get nothing whereas if you'd preallocated you'd have known before even starting that you didn't have enough memory.
I see no downside in preallocating, only benefits.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by