LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

is array building in a loop ALWAYS bad?

Oops- guess I should have initialized an array of 4 elements not 50 elements in that last example Smiley Wink


0 Kudos
Message 11 of 14
(1,013 Views)

Garvacious wrote:

 In other words, is there a need to overcomplicate things by coding something like the bottom loop?


No, the top example is perfectly fine. The compiler can figure out all final array sizes at compile time and allocate memory accordingly. The same buffers are re-used across all iterations.

Message 12 of 14
(1,003 Views)

altenbach wrote:
No, the top example is perfectly fine. The compiler can figure out all final array sizes at compile time and allocate memory accordingly. The same buffers are re-used across all iterations.

Yes, when working with constants. But what if the data to insert in the array comes from a DAQ card or from a calculation or from a TCP connection (string type-casted to array of DBL)? Then LV doesn't necessarily know the array size (even if it is constant).

Will it try to reuse the array if it fits and only allocate memory if the size would be larger than in the previous iteration?

 

0 Kudos
Message 13 of 14
(990 Views)

Thanks for all the response- I think Altenbach's last answer sums it up pretty well.  (I am assuming he was not making the assumption of having constants always populating the array).

 

I guess it was one of those old LabVIEW 101 misconceptions I carried around for years- "never use Build Array in a loop".  Of course I violated that rule all the time, but now I know for sure that it is not using the Build Array function that causes inefficient use of memory, but specifically changing the array's size.

 

Sometime's it's important to know how "smart" the LV compiler is- saves lots of unnecessary code optimization Smiley Happy



Message 14 of 14
(940 Views)