Effacer les filtres
Effacer les filtres

Why is pre-allocating done?

1 vue (au cours des 30 derniers jours)
S Priya
S Priya le 30 Juin 2021
Commenté : S Priya le 30 Juin 2021
In a program why is pre-allocating done?for example using arrays of zeros
ex- A=zeros(3)

Réponse acceptée

Yongjian Feng
Yongjian Feng le 30 Juin 2021
Hello Priya,
This is more like setting default values. Just in case, they are not set in some code paths, the default values will be there.
Thanks,
Yongjian

Plus de réponses (1)

Steven Lord
Steven Lord le 30 Juin 2021
See this documentation page for a brief discussion of why one may want to preallocate an array.
Imagine you were building a home. Which would be preferable?
  1. Buy a lot large enough to build the entire home.
  2. Build the bathroom.
  3. Build the bedroom.
  4. Build the kitchen.
  5. etc.
or
  1. Buy a lot large enough to build the bathroom.
  2. Build the bathroom.
  3. Buy a new lot large enough to hold the bathroom and a bedroom.
  4. Move the bathroom to the new lot.
  5. Build the bedroom.
  6. Buy a new lot large enough to hold the bathroom, a bedroom, and a kitchen.
  7. Move the bathroom and the bedroom to the new lot.
  8. Build the kitchen.
  9. etc.
Moving a building is hard. Ideally you don't do it, or you do it as few times as you can.
  1 commentaire
S Priya
S Priya le 30 Juin 2021
Thank you @Steven Lord

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by