LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Loop Control and Array Indexing

I'm using a loop to build an array, and have run into an interesting
problem. I need the ability to abort the loop in case of a failure,
but I'm not really willing to pay the penality (10x, due to
memory allocation, I suppose) of switching from using loop indexing
on a For loop to loop indexing on a While loop. Ideally, there'd be
a break feature in the For loop, but I haven't been able to find such a
feature in the documentation.

Any suggestions? Is preallocation of the array before I pass it into
a while loop the best I can do?

-Mike
0 Kudos
Message 1 of 4
(3,141 Views)
You'll have to do a test if speed is important. The 10x speed penalty you
mention is because the for loop allocates all the memory it needs and writes
into that memoryspace on the fly. Even if you preallocate the array
yourself, you get a significant speed decrease with "Replace Array Element"
when you have to use a while loop. The alternative is to put a case
structure inside the FOR loop and use a shift register so that you can trip
it into the "false" state where the loop iterates doing nothing for the
remainder of the count. This is generally considered less elegant than a
while loop, but it does save the overhead of Replace Array Element.

If the FOR loop executes *many* times you could try a combination; a WHILE
loop with a preallocated array and a FOR loop to
do, say, 100 chunks. After
each chunk comes out of the FOR loop, use Replace Array Element to copy it
to the allocated memory. I've never tried this though so I don't know what
the speed works out to.


Mike Schaeffer wrote in message
news:tnvt6.76234$lj4.2044070@news6.giganews.com...
>
> I'm using a loop to build an array, and have run into an interesting
> problem. I need the ability to abort the loop in case of a failure,
> but I'm not really willing to pay the penality (10x, due to
> memory allocation, I suppose) of switching from using loop indexing
> on a For loop to loop indexing on a While loop. Ideally, there'd be
> a break feature in the For loop, but I haven't been able to find such a
> feature in the documentation.
>
> Any suggestions? Is preallocation of the array before I pass it into
> a while loop the best I can do?
>
> -Mike
>
>
>
0 Kudos
Message 2 of 4
(3,141 Views)
"Is preallocation of the array before I pass it into a while loop the best I can do?"

This is good programming style. Its what happens (in the background) when you use autoindexing of the for loop. Labview preallocates an array based on how many iterations of the for loop are going to execute. If you are talking about the build array function, that's very wasteful. When you use build array you make a copy of the original array everytime you concatenate just one item onto it. Not a big deal when were are talking about a few elements, but think about an array with 1000 elements. You have the 1000 elements in memory along with the 999 array, 998 array and so on.

I think the while loop does not do that bad of a job of autoindexing either. According to the Labview man
ual, while loop autoindexing is nearly as efficient as for loop because it will increase array size in large chunks instead of 1 element each time around (G programming manual page 28-26).

For a very insightful look into how labview handles memory and some good examples, read chapter 28 of the G programming refernce manual.

Oh yeah, the for loop does not have a method for stopping like the while loop.

Jared
0 Kudos
Message 3 of 4
(3,141 Views)
I used to think pre-init was the way to go, but as you say simply
autoindexing from the while loop, as well as being easiest, is also the
fastest while loop solution. The attached benchmarker gives the times, in
milliseconds, to process a 10,000 element array 10,000 times by each method
and shows the overhead of the "Replace Array Element" function.


Jared wrote in message
news:506500000005000000B91D0000-984882144000@quiq.com...
> "Is preallocation of the array before I pass it into a while loop the
> best I can do?"
>
> This is good programming style. Its what happens (in the background)
> when you use autoindexing of the for loop. Labview preallocates an

> I think the while loop does not do that bad of a job of autoindexing
> either.
According to the Labview manual, while loop autoindexing is
> nearly as efficient as for loop because it will increase array size in




[Attachment LoopBenchmark.vi, see below]
0 Kudos
Message 4 of 4
(3,141 Views)