06-22-2010 10:29 AM
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...
-robweaver
06-22-2010 10:31 AM
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!
06-22-2010 10:39 AM - edited 06-22-2010 10:41 AM
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
06-22-2010 10:44 AM
@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 ![]()
06-22-2010 11:58 AM
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.
06-22-2010 12:56 PM
Thanks for the replies and clarifying that!
-robweaver
06-22-2010 02:00 PM
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.
06-22-2010 02:49 PM
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.
06-22-2010 02:54 PM
@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....
06-22-2010 03:04 PM - edited 06-22-2010 03:05 PM
@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!!! ![]()