01-23-2019 10:16 AM
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.....
Solved! Go to Solution.
01-23-2019 10:42 AM
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.
01-23-2019 10:52 AM
@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.
01-23-2019 10:55 AM - edited 01-23-2019 10:56 AM
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.
01-23-2019 10:55 AM
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).
01-23-2019 10:59 AM
@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.
01-23-2019 11:04 AM
@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.
01-23-2019 11:13 AM
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.
01-23-2019 11:17 AM
@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!
01-23-2019 11:18 AM
@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.