LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Best Practice: Using Constants with Loops

I've had this discussion with my team several times and we couldn't come to a final decision on what the best/most efficient practice is for using constants with loops? Should the constant be placed outside the loop and wired in through a tunnel? Should the constant be placed inside the loop (does the compiler recognize it as a constant and not initialize it each iteration)? Below is a very simple example of this, but I imagine it might be more critical with more complex loops/larger constants.

 

Just wanted to see what everyone's thoughts were...

 

17137i2356C88837086E7C

 

 

-robweaver

0 Kudos
Message 1 of 14
(10,251 Views)

They require the exact same amount of memory and are only loaded a single time.

In terms of run time, if there even is a difference, it would be so small you would never notice it.

So for all intents and purposes, I would say whatever works for you!

Cory K
Message 2 of 14
(10,244 Views)

The answer is "no differnce" since Loop invariant code motion moves the code that does not change, out of the loop. So it is a preference issue. In my case I try to keep the diagram neat so which ever version makes it neater would be my pref.

 

 

OT Cory your signature

 

Veni Vidi Necto  ... I came I saw I connected?

 

When the LV Champions where discussing thier moto, we were playing with that same phrase trying to figure out what "wired" ws in lattin when Aristos Queue recomended and we accepted;

 

"Veni Vidi VI"

 

Ben

 

 

 

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

 


@Ben wrote:

 

OT Cory your signature

 

Veni Vidi Necto  ... I came I saw I connected?

 

When the LV Champions where discussing thier moto, we were playing with that same phrase trying to figure out what "wired" ws in lattin when Aristos Queue recomended and we accepted;

 

"Veni Vidi VI"

 

Ben

 

 

 


 

Haha, yes it was supposed to roughly translate to "i came i saw i wired".

However, I dont think there were "wires" when Latin was around, so the closest translation I could find is:
"I came I saw I connected"

 

I think I like your suggestion better though Smiley Very Happy

Cory K
Message 4 of 14
(10,220 Views)

Quick thought:  As far as constants go, we all seem to agree no difference performance wise.  Two things I consider.

 

1)  I try to keep loops tidy and if I have a constant hanging around it sometimes forces me to have a bigger loop.  With it outside the loop I can often tuck it away without having to grow the loop.

 

2) I find myself changing some constants to controls (and vice versa) and if I already have it outside the loop then no need to rewire anything.

 

Corollary:  I do not like having to remember a bunch of rules, so by treating constants and controls the same (ie. outside), I have one less thing to think about.  All rules are made to be broken, so if that constant is really a constant (Pi isn't going anywhere), then as long as it looks good inside, then go for it.

 

Bottom Line:  No difference other than personal taste as far as I can tell.

Message 5 of 14
(10,191 Views)

Thanks for the replies and clarifying that!

 

-robweaver

0 Kudos
Message 6 of 14
(10,167 Views)

I also tend to place constants outside the loop for similar reason to Darin's.

 

Typically, I need a constant multiple times, so I tend to have each constant (with a catchy label!) towards the left of the diagram. This way they can be changed easily and I add wire branches if needed so it can reach all code and loops that utilize it.

Placing it inside a loop is also tedious if you later need it once more outside the loop, because it will autoindex at the output tunnel, requiring to change the tunnel mode.

0 Kudos
Message 7 of 14
(10,147 Views)

I like to use a numeric control set to have a default value to the value that I want and then I use a local variable to get the value.

0 Kudos
Message 8 of 14
(10,134 Views)

 


@smercurio_fc wrote:

I like to use a numeric control set to have a default value to the value that I want and then I use a local variable to get the value.


That is why he is a Knight.  As a mere mortal I would have used the Value Property to get the value, but locals are much faster....

 

Message 9 of 14
(10,130 Views)

 


@Darin.K wrote:

 


@smercurio_fc wrote:

I like to use a numeric control set to have a default value to the value that I want and then I use a local variable to get the value.


That is why he is a Knight.  As a mere mortal I would have used the Value Property to get the value, but locals are much faster....

 


Plus, they bug the hell out of Ray. Bonus!!! Smiley Very Happy

 

0 Kudos
Message 10 of 14
(10,121 Views)