11-11-2016 03:26 AM
Hi there,
(Please let me know if this post is duplicated)
I was looking for possible way to implement a While-Do loop from text programming language in LabVIEW, and I understand that the While Loop in LabVIEW is similar to a Do-While loop (Running the code in the loop first before checking the condition).
Here's a simple scenario:
I was to translate the following code in LabVIEW:
while (x > 0) { // Loop Code goes here x--;
// Note: Bear in mind the above line is just for simple scenario.
// There are cases where code within the loop involves calculate x value }
While I already have an idea in how to implement this in LabVIEW, I came across a LabVIEW Help link (along with other forum posts with solution) that suggested using Case Structure inside a While Loop.
http://zone.ni.com/reference/en-XX/help/371361N-01/lvhowto/preventing_code_execution/
Which means the code will be something like this:
Although it is not wrong, but I believe this can be inefficient especially when running lots of iterations and execution speed is a concern. The While Loop will always running the Case Structure for every iteration and may introduce some overhead.
So as alternative, instead of placing the Case Structure inside a While Loop, how about placing the While Loop inside a Case Strcuture?
With that you can run the Case Structure only once to check the condition first, before running the While Loop if the condition is true.
After tried running both methods, it seems that the alternative method runs faster than the method described in the LabVIEW Help site.
I've attached the code below so that you can give it a try.
Should you have any comments or a much better alternative to implement a While-Do loop in LabVIEW, feel free to discuss and share with us.
Thank you.
Regards,
Ong Ee Lim
11-11-2016 06:46 AM
There are many things that are often balanced when dealing with optimizing code. In this case, you have the ease of editing and code simplicity versus execution speed.
What happens if the run condition is changed? You have to update the code in two places. Is that really worth the very small gain you will have by not having the case structure inside of the loop? For most applications, I would say not. Only when I really need to tweak every bit of performance out of a loop would I do somthing like this. And even then, there are many many more optimizations I could do before this.
With that said, I would really like to see some real benchmarks. That would better quantify the performance gain.
11-11-2016 11:53 AM
LabVIEW has apparently influenced the way I program over the years, and changed how I think about these types of problems. As a result I've never thought I needed the other type of while loop, and I'm struggling to come up with a real world time that I would have used it recently, had the feature somehow existed in LabVIEW.
Unofficial Forum Rules and Guidelines
Get going with G! - LabVIEW Wiki.
17 Part Blog on Automotive CAN bus. - Hooovahh - LabVIEW Overlord