ni.com is currently experiencing unexpected issues.

Some services may be unavailable at this time.

取消
显示结果 
搜索替代 
您的意思是: 

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

已解决!
转到解答

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.....

下载全部
0 项奖励
1 条消息(共 12 条)
3,773 次查看

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 项奖励
2 条消息(共 12 条)
3,765 次查看

@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 项奖励
3 条消息(共 12 条)
3,754 次查看

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 项奖励
4 条消息(共 12 条)
3,750 次查看

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 项奖励
5 条消息(共 12 条)
3,749 次查看

@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 项奖励
6 条消息(共 12 条)
3,742 次查看

@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 项奖励
7 条消息(共 12 条)
3,734 次查看
解答
已被主题作者 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
8 条消息(共 12 条)
3,724 次查看

@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! 

9 条消息(共 12 条)
3,721 次查看

@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
10 条消息(共 12 条)
3,719 次查看