LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to avoid Build Array function inside a For (while) Loop?

Hi there,
I have a simple question about how to avoid using Build Array function inside the loop. Now I want to remove the Build Array funtion inside the loop to improve the performance (To get better memory management). Any idea how to do that?
Thanks a lot!
 
Warmest regards,
Chong
0 Kudos
Message 1 of 8
(4,813 Views)
By default, when you wire something out of a for loop, auto-indexing is enabled and an array is automatically generated. Because the compiler knows the size of array to create (either by a value wired to the count terminal or the size of an auto-indexed array input), memory management is very effecient.
Message 2 of 8
(4,802 Views)
If you're using a While loop instead of a For loop, you might want to look at this thread.

Basically you initialize an array to a size you think you won't go over, and watch the array and only add more space if needed. I made some example VIs that you can download from the other thread.

Ed


Ed Dickens - Certified LabVIEW Architect
Lockheed Martin Space
Using the Abort button to stop your VI is like using a tree to stop your car. It works, but there may be consequences.
0 Kudos
Message 3 of 8
(4,795 Views)
If you have to handle this inside of the loop and you know the array size, use initialize array and replace the array elements instead of building the array. If you don't know the size, but you know it will be big, you can do a combination of initializing the array to a specific size and if it reaches that size, enlarge it yourself using Build Array. This way you can probably avoid the allocation going on when the array is smaller, but you need to test this.

___________________
Try to take over the world!
0 Kudos
Message 4 of 8
(4,789 Views)
For a test long ago, I had to generate words to write to a 4MB memory device. Simply using a For loop to generate the array pattern for this took a long time since the array was dynamically built. Because this large array was a known length in size, I discovered later that it was much more efficient to initialize the array outside of the loop. The initialized array was fed into the loop's shift register and I filled in each element with the replace array subset vi rather than a build array vi. This sped up the word generation significantly.
0 Kudos
Message 5 of 8
(4,790 Views)

Dear friends,

Thank you very much for the quick responses.

For while/for loops I have tried using two methods: auto-indexing;  and initialize the array outside the loop and replace the array subset inside the loop with shift register. Is there any difference between these two, such as the speed and memory allocation? I am dealing with very big array.

Thanks.

0 Kudos
Message 6 of 8
(4,752 Views)
It's been my experience that using the auto-indexing to build an array on a For loop is just as good as initializing and replacing elements. The For loop knows before it runs how many iterations it has to run and can allocate the array ahead of time.

You're better off initializing and replacing when using a While loop because it does not know how many iterations it will run and can't pre-allocate the array ahead of time.

Ed


Ed Dickens - Certified LabVIEW Architect
Lockheed Martin Space
Using the Abort button to stop your VI is like using a tree to stop your car. It works, but there may be consequences.
Message 7 of 8
(4,737 Views)

Dear Mr. Dickens,

Thank you very much.

Have a good day.

 

Sincerely,

Chong 

Message 8 of 8
(4,726 Views)