LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

memory benefits using conditional For Loop

I am curious if there are any benefits for memory efficiency when using the conditional for loop instead of the while loop?  Seems like there must be a reason that this tool exists.

Cheers!
0 Kudos
Message 1 of 10
(3,656 Views)
Hi!
   I think while loop is one of the most efficient (and simple) constructs in almost every programming language.

   I suppose that For loop, used with conditional terminal have been introduced in LabView for give the same opportunity  you have in other programming languages to exit a for loop even if not all the iteration are done, I mean the "break" instruction in C programming language, that can be used to exit also a for loop (even if most known with switch-case construct).

   Hope this help

graziano
Message 2 of 10
(3,652 Views)
Other than the fact the conditional stop in a for loop makes certain kinds of operations MUCH simpler, I seem to remember that the developer who worked on them said that he was surprised by how much more efficient this was when compared to doing it in LabVIEW code. Even if it wasn't, it would probably be worth using just for the simplified code.

I can't find a reference, so I'm guessing that this was in the beta discussions for the version which introduced this feature.

___________________
Try to take over the world!
Message 3 of 10
(3,631 Views)

In regards to the conditional for loop, I am curious if the memory footprint of a conditional for loop is different than the memory footprint of a while loop doing the same thing.  See below for an example.



It seems to me that there is a difference in the way that memory is allocated between the two as with the for loop a chunk of memory the same size as the input array is being set aside for the output array while with the while loop that chunk of memory is not set aside until the size of the output is known.  It seems to me that if the input Array were large enough and the output array ended up being small enough that the while loop would be more efficient.  Im not trying to be nitpicky here I am just trying to get a better understanding for how LabVIEW is allocating memory. 

Cheers!


Message Edited by jmcbee on 01-22-2008 12:28 PM
0 Kudos
Message 4 of 10
(3,621 Views)


jmcbee wrote:

It seems to me that if the input Array were large enough and the output array ended up being small enough that the while loop would be more efficient.
That appears to be a fair assesment, although the exact details are probably liable to change
and you would need to run some benchmarks to find when using a while loop would be better.
 
In any case, you should note that your two pieces of code are not equivalent. To achieve the same functionality with the while loop, you would need to wrap it in a case structure (to handle the case of an empty array) and add another condition for i=Array Size-1.

___________________
Try to take over the world!
Message 5 of 10
(3,610 Views)
You are correct about the while loop needing some more work, I was focused on trying to get a devils advocate for the for loop and neglected robustness so to say.  As I mentioned I am  trying to get used to thinking about memory allocation while I program, it is easy to neglect this with LabVIEW.

Cheers!
0 Kudos
Message 6 of 10
(3,604 Views)


@jmcbee wrote:


It seems to me that there is a difference in the way that memory is allocated between the two as with the for loop a chunk of memory the same size as the input array is being set aside for the output array while with the while loop that chunk of memory is not set aside until the size of the output is known. 


In the while loop, the output array size is not limited (a priori), I mean, you can have more execution cycles than in the for loop, in which the maximum number of cycles is limited by the length of the input array, but I don't know LabView internals, so I think the best way is to benchmark, I'll see something this evening.

graziano
0 Kudos
Message 7 of 10
(3,573 Views)
If you are working with big arrays the Contional For Loop will be almost always faster.
It preallocates the memory it needs and that is the best.
The While Loop will be faster only if the condition that stops the loop, will happen in a few iterations and the Contional For Loop has preallocate a large numer of memory..
 
0 Kudos
Message 8 of 10
(3,567 Views)

Saying much of the same but in my own words.

The For Loop has since LV 6.0 (or there-abouts) been able to allocate the buffer used for the auto-indec output tunnel one time before the loop starts.

The While loop CAN'T do this because it has no idea how big the array wil be. Therefore as the output tunnel array grows, the while-loop operation has to accationally stop and ask for more memory from the OS.

I also beielve I read somewhere that the condition to stop the For Loop is slightly more efficient than the explicit code required in the While loop. But I can not cite a reference to back up this belief.

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 9 of 10
(3,553 Views)

Would just add that memory SIZE efficiency will not necessarily coincide with memory SPEED efficiency.  Memory allocation operations can be very very very much slower than memory access / copy operations.  In the RT world, they add a lot of jitter or indeterminism to your execution rate.  The best choice will vary with circumstance, but in general I think there's more chance for a While loop to become inefficient than a For loop.

As a general rule of thumb, I'd probably use* a conditional auto-indexed For loop much of the time.  With While loops, one can overlook degenerate cases and end up with an infinite loop.  For loops are sure to be bounded.

-Kevin P.

* - haven't used one yet though.  Still getting my feet wet in 8.5, and haven't yet needed the construct.

 

CAUTION! New LabVIEW adopters -- it's too late for me, but you *can* save yourself. The new subscription policy for LabVIEW puts NI's hand in your wallet for the rest of your working life. Are you sure you're *that* dedicated to LabVIEW? (Summary of my reasons in this post, part of a voluminous thread of mostly complaints starting here).
Message 10 of 10
(3,538 Views)