LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is it slower to use subvi's instead of just the code all in one file?

Solved!
Go to solution

What I've been doing lately when I have a lot of code/wires that can be grouped together, I've been putting that into a subVI. I've got another file with a tonnnn of messy wires in it, but I would probably be splitting it up into maybe 3 subVI's for all the different calculations. And then outputting a cluster.

 

Like the title says, is it slower to use subVI's? Am I slowing down my loop speeds by forcing the program to open a subVI?

0 Kudos
Message 1 of 14
(2,138 Views)

@David99999 wrote:

Like the title says, is it slower to use subVI's? Am I slowing down my loop speeds by forcing the program to open a subVI?


Short answer: No.

 

More nuanced answer: The subVI call overhead is so tiny that it is basically non-existant.


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 2 of 14
(2,132 Views)
Solution
Accepted by topic author David99999

You probably won't see much of a difference, but the problem is complicated and multidimensional

 

  • There is a certain subVI calling overhead, but it is probably negligible compared to the rest of the code.
  • You could inline the subVI, but that can cause other problems.
  • If the diagram (current VI and all inlined subVI exceeds a certain complexity, the computer will reduce the optimization efforts in order to reduce the compile time, so the compiled code is less efficient. If you keep subVIs reasonable, they will all compile with full optimizations.

 

The correct way is to do a better program design from the ground up. Use subVIs for code that is used in several places. Define their behavior and fully debug them so the you can focus on the main logic in the toplevel. There is rarely a need for gigantic diagrams.

 

SubVIs also should only have a limited number of well defined and labeled inputs and outputs. Just creating subVIs from a huge pile of spaghetti code will blur all the code logic and make things even harder to debug.. 

 

 

Check the code complexity in VI options:

 

altenbach_0-1659366171884.png

 

Adjust the threshold if you prefer optimizations over compile time:

 

altenbach_1-1659366258972.png

 

We probably could give you much more specific advice after seeing your code. 😄

 

Message 3 of 14
(2,126 Views)

Breaking code up into subVIs based on function is a good reason to create a subVI as well.

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.
Message 4 of 14
(2,122 Views)

That is a good question But the answer is very complex.  I'll try to hit some of the general points and see if I can help you refine what you are concerned about in your code.

 

There is a cost in time for a vi call.  It can vary a lot under certain conditions:

  • An inlined subvi is actually compiled into the owning block diagram and has no "call setup" overhead but does add a synchronization boundary (waiting for all inputs to be available before starting and all outputs to be calculated before exiting) trivial.
  • A subvi that is not inlined will have a call setup depending on the allow debugging property. There is simply some extra things that have to be done to let you set probes, breakpoints and various other things. 
  • A reenterant vi must first check a dataspace out of the Clone pool if one is available or create a new dataspace if one is not prealocated.  Creating a new dataspace can be costly in time but there is a method to preallocate as many as you will need.
  • A dynamic dispatch vi has a little longer setup since the runtime class implementation must be resolved
  • Calling a vit as a subvi is extremely costly and should not be done anymore. Use an Asynchronous Call By Reference instead.  The ACBR node was not available many years ago and some legacy code can still be found wandering out in the wild

This list is not exhaustive but, illustrates the complexity of the answer.

 

Generally though, there are very few cases that I have ever come across where absolute performance of a program was impacted more by a subvi call than other code concerns such as;

  • Overburden of the UI Thread
  • Unnecessary data copies ( When you pass "MyClustersaurus Rex" into a subvi that only operates on one element)
  • Repeatedly resizing data
  • Not parallelizing for loop when useful
  • Poor front panel design
  • Rube-Goldburg code (when you write 5 square yards of code to give you the same result that you could fit on a postage stamp)
  • ......

 


"Should be" isn't "Is" -Jay
Message 5 of 14
(2,109 Views)

@David99999 wrote:

What I've been doing lately when I have a lot of code/wires that can be grouped together, I've been putting that into a subVI. I've got another file with a tonnnn of messy wires in it, but I would probably be splitting it up into maybe 3 subVI's for all the different calculations. And then outputting a cluster.

 

Like the title says, is it slower to use subVI's? Am I slowing down my loop speeds by forcing the program to open a subVI?


Since you ask this question, you must be a beginner and to keep things simple, as other experts have commented, the answer is "NO"

 

The answer becomes a "Maybe" if you call the same subVI in more than one section of code in parallel and the subVI takes a considerable amount of time to execute, now the flat VI method would have been faster. But that is not all, you can change the re-entrancy of the subVI (under careful consideration of what is inside it) to make it as fast as the flat code.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
Message 6 of 14
(2,061 Views)

Everyone before me covered the technical points, so here's my high-level take:

 

If you're at a point where you think the call time of a subVI is limiting the performance of your application, then you have bigger problems to worry about than subVI call times.

 

Most likely the actual problem would truly be one of:

  • You need a new architecture
  • You need faster PC hardware
  • You need to enable more parallel processing
  • You need specialized hardware (FPGA or other real-time)
  • You're using a wildly inefficient algorithm
  • You've got something else on your PC slowing it down, such as an overly aggressive antivirus
  • You're waiting on a network or other external device and that's the actual delay
  • You've either got a memory leak or your code is causing huge amounts of buffer allocations to be needed

 

Or something else along those lines.

Message 7 of 14
(2,028 Views)

@David99999 wrote:

What I've been doing lately when I have a lot of code/wires that can be grouped together, I've been putting that into a subVI. I've got another file with a tonnnn of messy wires in it, but I would probably be splitting it up into maybe 3 subVI's for all the different calculations. And then outputting a cluster.

 

Like the title says, is it slower to use subVI's? Am I slowing down my loop speeds by forcing the program to open a subVI?


Why not try it and see for yourself? Make a loop with a bunch of code in it, time that. Then select all the code in the loop, click 'create sub vi from selected code' then time it again. 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
Message 8 of 14
(2,023 Views)

@Jay14159265 wrote:
Why not try it and see for yourself? Make a loop with a bunch of code in it, time that. Then select all the code in the loop, click 'create sub vi from selected code' then time it again. 

Creating a valid benchmarking harness is full of landmines. There is a very high change that the results are meaningless. 😄 

Message 9 of 14
(2,011 Views)

@altenbach wrote:

@Jay14159265 wrote:
Why not try it and see for yourself? Make a loop with a bunch of code in it, time that. Then select all the code in the loop, click 'create sub vi from selected code' then time it again. 

Creating a valid benchmarking harness is full of landmines. There is a very high change that the results are meaningless. 😄 


There's also a very high chance that it will illustrate how in the weeds the question is.

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.
Message 10 of 14
(2,005 Views)