LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Which is the more efficient way to feed a constant into a loop: wire or feedback node?

Solved!
Go to solution

I'm writing (or rather rewriting) a big program and I need to feed a minimum, maximum, and initial value into a loop to ensure any set value stays within those parameters (these are values for current, voltage, and temperature in case anyone is curious). If I understand it right, wires are always always always the most efficient way to transfer the data, but stretching a bunch of wires across my program, well.....it's gonna look ugly as hell and I'm trying for maximum readability (so I don't have to be called to fix it after I graduate).

Here's the question:

Is there a large drop in efficiency if I use feedback nodes rather than wires? It'll save me stretching wires all over the place, but I've been eliminating feedback nodes and shift registers when not necessary to try to stream line this thing.

Attached is a picture of an example program and the actual program. Any thoughts would be greatly appreciated. Thanks in advance!

Addendum: in the picture, I have simple values for min and max, but in actuality I'll have some calculations out there. Nothing fancy, but I'm trying to avoid doing that math every loop....unless it doesn't matter and I'm overthinking this.....

Download All
0 Kudos
Message 1 of 12
(3,486 Views)

Why not put the constants inside the loop right next to the In Range function?  No reason to have them outside if they are constant. 

aputman
0 Kudos
Message 2 of 12
(3,478 Views)

@aputman wrote:

Why not put the constants inside the loop right next to the In Range function?  No reason to have them outside if they are constant. 


It's probably a little OCD of me, but I'm avoiding doing a minor calculation every loop. The loop is going to run for hours or days. While calculating a percent of the initial value to get my allowed range isn't a difficult calculation, I worry it'll add extra time, you know?

It's seriously, like, 10% or the initial current and 2% of the temp determines my range. Voltage requires a call to a laser controller, but I could find a way around making that call over and over.

0 Kudos
Message 3 of 12
(3,467 Views)

As a rule, I usually have my constants inside the loop.  If the value is coming from a control, I'll have the control outside the loop.

 

I actually don't know where this came from; it's just something I do.  I may have read something about why in the distant past, but it is beyond me now.

Bill
CLD
(Mid-Level minion.)
My support system ensures that I don't look totally incompetent.
Proud to say that I've progressed beyond knowing just enough to be dangerous. I now know enough to know that I have no clue about anything at all.
Humble author of the CLAD Nugget.
0 Kudos
Message 4 of 12
(3,463 Views)

Your feedback nodes make no sense because the values never change. Having the constants inside or outside the loop makes no difference, the compiler will create identical code (loop invariant code motion).

0 Kudos
Message 5 of 12
(3,462 Views)

@altenbach wrote:

Your feedback nodes make no sense because the values never change. Having the constants inside or outside the loop makes no difference, the compiler will create identical code (loop invariant code motion).


I only wanted to use feedback nodes to improve readability; I recognize the choice makes no sense if you just consider whether the value changes. The program is a little big and I end up stretching wires across the whole thing. Feedback nodes would just eliminate the wires, but I'm worried about unnecessary overhead.

0 Kudos
Message 6 of 12
(3,455 Views)

@Jantzi wrote:

The program is a little big and I end up stretching wires across the whole thing.

That tells me you have not broken up your code enough into subVIs, parallel loops, states (in a state machine architecture), etc.  A general rule is that a VI's block diagram should be no bigger than a single screen.


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 7 of 12
(3,447 Views)
Solution
Accepted by Jantzi

If you are stretching wires all over it sounds like you need more improvements to your code. Are you using a state machine or some other general architecture to structure your code? For larger, more complex code (usually top level control loops) I will bundle my general parameters into a cluster and pass a single wire through the code. This does mean I need to unbundle to access values but it minimizes the number of wires routed through the code. Also, unless you are under some extreme timing constraints don't over burden yourself trying to optimize your code to extremes. While it is true if your calculation will always produce the same result it should go outside of the loop but you may find that you overthink optimizing your code for very little gain. This can lead to less readable code.



Mark Yedinak
Certified LabVIEW Architect
LabVIEW Champion

"Does anyone know where the love of God goes when the waves turn the minutes to hours?"
Wreck of the Edmund Fitzgerald - Gordon Lightfoot
Message 8 of 12
(3,437 Views)

@Jantzi wrote:

 

but I'm worried about unnecessary overhead.

As explained, constants have no overhead, no matter the placement. Also, operations on scalars are dirt cheap. Nanoseconds! 

Message 9 of 12
(3,434 Views)

@Jantzi wrote:

@aputman wrote:

Why not put the constants inside the loop right next to the In Range function?  No reason to have them outside if they are constant. 


It's probably a little OCD of me, but I'm avoiding doing a minor calculation every loop. The loop is going to run for hours or days. While calculating a percent of the initial value to get my allowed range isn't a difficult calculation, I worry it'll add extra time, you know?

It's seriously, like, 10% or the initial current and 2% of the temp determines my range. Voltage requires a call to a laser controller, but I could find a way around making that call over and over.


A percent calculation is what...2 machine instructions?  (Sorry, it's been a while since I took those classes).  If you think your program is that time critical... it's not.  Don't overthink it too much. 

aputman
Message 10 of 12
(3,432 Views)