I'm aware its best practice to pre-allocate large arrays to avoid growing them. Not doing so slows down execution as explained on the preallocation documentation page.
However, when an array is an element of a structure, it seems that it doesn't much matter whether preallocation is done or not. Code on arrays in structures is also significantly slower than when the array is not in a structure. I built upon the example on the preallocation doc page to illustrate:
Results in the following:
Elapsed time is 0.166783 seconds.
Elapsed time is 0.008202 seconds.
Elapsed time is 0.731128 seconds.
Elapsed time is 0.531887 seconds.
Processing the pre-allocated array (case 2) is must faster than when the array grows (case 1). The last 2, which utilized a non-preallocated and a preallocated array in a structure, take approximately the same amount of time. Both are significantly slower than their non-structure counterparts (the first two). My questions are, should arrays in structures be pre-allocated, is the lack of efficiency with pre-allocation expected for arrays in structures, and is the overall slowdown (pre-allocation or not) expected when the array is in a strucutre?
Thanks for any assistance.