LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

efficiency question, looping or brute force?

Solved!
Go to solution

hey guys,

I dont really consider myself to be a programmer, more like someone who has the ability to stumble through and make things work. I am however always trying to better my working practices.

I was thinking about this question the other day and the oppertunity to select either option just came up in a project im working on. when I'm asking this question I'm interested more in a best general practices context not really an " i need this code to run as fast as possible" context.

 

the problem I have is I need to run a sub vi twice to access the two motors on my robot, the command to access the first motor from the second motor is one away from each other, ie. motor one is command 5 and motor 2 is command 6. my first idea was to create a for loop and add the first motors offset to the loop index and run the loop for two itterations.

 

I then had a coworker come look over my shoulder and ask why I didnt just create the sub vi twice and not use the loop.

 

I realize these accomplish the same thing but I like to try and better myself as I said above, what are the pros and cons to each method and is one preferred over the other?

 

thanks guys

0 Kudos
Message 1 of 7
(3,718 Views)

Unless you specifically command LabView to do the opposite, SubVIs behave as "one user" which means, if several sections of your code want to use the same SubVI, only one of them can actually do so, while the others must wait for that section to be finished. Which one uses the SubVI first can be decided via coding, and if you do not, LabView automatically chooses whichever one seems like the most viable instance. This is useful to avoid race conditions and, depending on your code, you might want to do exactly that.

 

From a readability point of view, I would prefer the "call SubVI twice" method opposite to the loop, because when looking at code, it is much easier to say "oh ok, he calls the SubVI for one motor, then the other" instead of "wait why does he run this in a loop? Oh, that's why". Also keep in mind that the loop will lock the code until it finishes if any outputs it has are required further along the coding.

Message 2 of 7
(3,705 Views)

@Daikataro wrote:

From a readability point of view, I would prefer the "call SubVI twice" method opposite to the loop, because when looking at code, it is much easier to say "oh ok, he calls the SubVI for one motor, then the other" instead of "wait why does he run this in a loop? Oh, that's why". Also keep in mind that the loop will lock the code until it finishes if any outputs it has are required further along the coding.


I actually find the FOR loop easier to understand and it is more extensible.  What helps is if you make the label visible and you put a comment that you are looping over the motors.  The only difference I have from the OP's implementation is use an array constant for the modes and just autoindex on the array.

 

As far as performance is involved, it sounds like we are dealing with some hardware layer.  So something is likely to block one from running, even if at the driver level.  So going with a parallel execution won't help much at all.  So go for readability.


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
Message 3 of 7
(3,686 Views)

oh, I didn't think about the auto indexing property, that's an even better idea. I do also always include notes around things I do like that.

 

So, from your two replies it seems like its just a user preference things and as long as its documented well enough it really makes no difference.

thank you guys for othe replies

0 Kudos
Message 4 of 7
(3,675 Views)
Solution
Accepted by topic author pathare535

User preference is certainly a part of it, but when it comes to "best practices", using the for loop seems the better option if only for the sake of extensibility. In the case where you plop the subvi down twice, you'd have to wire an individual value to each. However, with a for loop you could programmatically create an array and feed it to the same subvi as many times as you wanted to. This would make adding devices easier.

Message 5 of 7
(3,661 Views)

Adding to the mix...

 

If the sub-VI is re-entrant you can invoke both aht the same time.

 

Parallized For Loops calling re-entrant subVIs...

 

But if the re-entrant sub-VIs use another sub-VI that is not re-entrant OR a dll that is configured to run in the UI Thread...

 

Back in the day...

 

Prior to the paralle For loop I did restort to dropping multiple re-entrant sub-VIs on a diagram so that I use multiple cores to crunch numbers.

 

Ben

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 6 of 7
(3,656 Views)

@Daikataro wrote:
Also keep in mind that the loop will lock the code until it finishes if any outputs it has are required further along the coding.

What makes you think that's any different than the subVIs?

0 Kudos
Message 7 of 7
(3,624 Views)