LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

While loop VS for loop

I am enabling the index and getting the values inside both for loop and while loop.Which one do you guys think is efficient and a proper coding style.I maybe wrong but i feel while loop to be much faster than for loop.Is that true?
0 Kudos
Message 1 of 3
(4,605 Views)


rpr wrote:

i feel while loop to be much faster than for loop.Is that true?

No, they both run at basically the same speed given the same conditions. If anything, the while loop will run a bit slower because it has a condition to evaluate with every iteration.
 
The basic rule is simple - you use a for loop if you want to run a known number of times. Otherwise, you use a while loop. There is one exception and that's using the conditional terminal on the for loop. You can see some details here.
There are two performance enhancments which a for loop can have - because it is run a known number of times it can have the memory for its outputs preallocated (which makes a big difference) and on certain occasions it can be computed before the VI runs.

___________________
Try to take over the world!
Message 2 of 3
(4,602 Views)
Thanx for the explanation Tst.
0 Kudos
Message 3 of 3
(4,599 Views)