LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

FGV with FOR loop?

Hi guys

 

All the implementations of FGV's that I have seen, are with a While loop with a T wired to its conditional terminal. 

 

I was wondering if using a FOR loop with N=1 makes any difference. 

 

As far as I have read, there should be no difference. But are there any finer points like memory optimization/timing constraints etc, that may need to be considered? Is there any particular reason why While loops are used exclusively for FGVs?

0 Kudos
Message 1 of 8
(2,894 Views)

@t.d.b wrote:

All the implementations of FGV's that I have seen, are with a While loop with a T wired to its conditional terminal. 

 

I was wondering if using a FOR loop with N=1 makes any difference. 

It makes no difference in functionality.

 

You can also use a feedback node. The wiring gets a bit more messy though, but you have more control over the initialization (both value as condition). A good option to know.

 

You can also use local variables, but this will keep the FP in executables. And each local will make a copy. So, a lesser solution.

 


@t.d.b wrote:

As far as I have read, there should be no difference. But are there any finer points like memory optimization/timing constraints etc, that may need to be considered? Is there any particular reason why While loops are used exclusively for FGVs?


AFAIC, it's just tradition to use a while loop.

 

Traditions are part of any language though. If you use a while loop, people will understand at a glance. With a for loop, it will take a second look.

 

A while loop suggest iterating until a condition is true or false. A for loop suggest iterating over something. Although the iterations can be set to 1, intuitively, a while loop is closer to the job at hand: loop until a condition is true or false. But it's in the gray area...

0 Kudos
Message 2 of 8
(2,887 Views)

wiebe@CARYA wrote:

You can also use a feedback node. The wiring gets a bit more messy though, but you have more control over the initialization (both value as condition). A good option to know.


99% of my Action Engines use the Feedback Node.  It just seems cleaner to me to not have the loop.  But once I try to hold more than 1 thing, the Feedback Node method gets messy and I will switch to the While Loop.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 8
(2,856 Views)

@crossrulz wrote:

wiebe@CARYA wrote:

You can also use a feedback node. The wiring gets a bit more messy though, but you have more control over the initialization (both value as condition). A good option to know.


99% of my Action Engines use the Feedback Node.  It just seems cleaner to me to not have the loop.  But once I try to hold more than 1 thing, the Feedback Node method gets messy and I will switch to the While Loop.


I found feedback nodes to be slower in some cases. But the control over initialization is a bug bonus.

 

Another bonus is the (optional) enable input. With a shift register, you can do that with a Select, but with the FBN it's build in.

0 Kudos
Message 4 of 8
(2,851 Views)

I found feedback nodes to be slower in some cases. But the control over initialization is a bug bonus.

Hoping it's not a bonus bug! 😁

Paolo
-------------------
LV 7.1, 2011, 2017, 2019, 2021
0 Kudos
Message 5 of 8
(2,837 Views)

wiebe@CARYA wrote:

I found feedback nodes to be slower in some cases.


I'm curious about your cases.  I remember AristosQueue posting a thread where he did the speed comparisons between a Feedback Node and While Loop with Shift Register with NI's massive benchmarking system (unable to find the thread at the moment).  I recall the results were that the Feedback Nodes were very slightly faster than the loop, hardly enough to worry about.  But that was sever years ago.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 6 of 8
(2,823 Views)

I thought I remembered a similar comparison which found the 1-iteration For Loop to have a very slight advantage over the traditional While Loop.  Can't find that thread either.   I think the speculation was simply that because For Loops in general have more *opportunity* to be optimized than While Loops, the LabVIEW internal compiler can sometimes produce more efficient code from a 1-iteration For Loop than a 1-iteration While Loop.

 

 

-Kevin P

ALERT! LabVIEW's subscription-only policy came to an end (finally!). Unfortunately, pricing favors the captured and committed over new adopters -- so tread carefully.
0 Kudos
Message 7 of 8
(2,813 Views)

@crossrulz wrote:

wiebe@CARYA wrote:

I found feedback nodes to be slower in some cases.


I'm curious about your cases.  I remember AristosQueue posting a thread where he did the speed comparisons between a Feedback Node and While Loop with Shift Register with NI's massive benchmarking system (unable to find the thread at the moment).  I recall the results were that the Feedback Nodes were very slightly faster than the loop, hardly enough to worry about.  But that was sever years ago.


I used the FBN enable, so that might be a factor. In the while loop, this was handled in a case propagating the old value or selecting a new value. The FBN used the enable, and I suspect the condition forced a copy 'into' the FBN, while the loop was either a no-op or setting a value to the pointer.

 

It was complex though, with ~6 values, all independently enabled. The values where complex too, class objects and queue references.

0 Kudos
Message 8 of 8
(2,804 Views)