Jump to content

LV2-style globals: Ever wondered whether to use While Loops or For


Recommended Posts

Almost every time I see a someone write an LV2-style global (you know, an uninitialized shift register on a loop that is guaranteed to run exactly once, used to store data for retrieval on a later call), it is written with a While Loop, with a constant wired to the Stop terminal. I've always written mine with a For Loop with a constant 1 wired to the N terminal. It seemed more intuitive, and for some reason I felt LV might optimize it better, since normally the contents of a While Loop cannot be optimized based on number of times it'll execute, but a For Loop always can.

So I asked Jeff K (father of LV, deep knowledge of diagram optimization). He says that we can indeed constant-fold a While Loop with a constant wired to the Stop terminal. It's the only time we can constant fold the contents of a while loop, but we can do it. He says that there's no advantage to using one over the other.

It answered a nagging question in my mind. I thought others might be interested as well.

Link to comment
  • Replies 65
  • Created
  • Last Reply

Top Posters In This Topic

I honestly never thought of using a for loop. I always used a while loop since the beginning of time. What's even more curious is that of all the LabVIEW programmers I've interacted with, not even once has anyone brought this point up. I'm not trying to suggest that your implementation is invalid or any less worthy. I'm just shocked that this is the first time I've come across this. Learn something new everyday, I guess.

Link to comment

QUOTE(Aristos Queue @ Mar 29 2007, 05:30 AM)

So I asked Jeff K (father of LV, deep knowledge of diagram optimization). He says that we can indeed constant-fold a While Loop with a constant wired to the Stop terminal. It's the only time we can constant fold the contents of a while loop, but we can do it. He says that there's no advantage to using one over the other.

I tested this particular issue a month back while I was working on a piece of code that would use in everywhere. The result was that while loop indeed is faster than for loop for this particular task. I didn't figure out why, after all I don't have deep knowledge of diagram optimization like Jeff K does.

Tomi

Link to comment

QUOTE(Jim Kring @ Mar 29 2007, 12:56 AM)

I prefer not to use loops, at all -- just use a unowned (floating) feedback node.

James,

Interesting use of the feedback loop. This use doesn't seem to be covered in the LabVIEW documentation at all.

Could you elaborate slightly as to what is going on?

Thank you.

Jon

Link to comment

I do not remeber which verion of LV it was but I benchmarked the performance of the For vs While thinking that I could pick up some speed with the For loop.

To my suprise the While loop won.

Another point (that I was planning to cover in a future Nugget) is that AE do not have to limit themselves to a single iteration.

Example:

In order to "shoe-horn" an app into a FP 2000 I needed to be very careful with my memory usage. By using an AE that could call itself, all of the analysis could be performed with the data residing in the SR. It sorta works like a "state machine" with multiple entry states.

SInce the actual proccessing required depended on the data, I could not tell before the AE was called, how many steps would execute.

So...

Using a while loop allowed me to adapt without replacing the while with a for.

Just my thoughts,

Ben

Link to comment

QUOTE(Jim Kring @ Mar 29 2007, 01:56 AM)

Hmmm... Just looking at the scrollbars there is a hint that the diagram is larger than what is shown...

Anything hidden in the 5th dimension, Jim?

Link to comment

QUOTE(Tomi Maila @ Mar 29 2007, 03:28 AM)

I tested this particular issue a month back while I was working on a piece of code that would use in everywhere. The result was that while loop indeed is faster than for loop for this particular task.

Do you have the VIs that you used for the benchmarking? These would be useful to investigate.

Link to comment

I did one with a FOR loop once, quite a while back. The application was a sort of Tag list, which was rather large, but the individual reads and writes were allowed to be sparse arrays. The actual read or write variable was a cluster of two arrays, one for indexes and one for the data. This allowed for non-contiguous get-set of data chunks. The FOR made this a little simpler, although it could have been written easily enough with a While.

Link to comment

QUOTE(Aristos Queue @ Mar 29 2007, 04:36 PM)

Do you have the VIs that you used for the benchmarking? These would be useful to investigate.

I didn't find the old tests but I quickly wrote new ones. They are attached. Feedback node was fastest, while loop second and for loop last.

Tomi

Link to comment

QUOTE(Tomi Maila @ Mar 29 2007, 08:38 AM)

Feedback node was fastest, while loop second and for loop last.

Now *that* is interesting... Seeming that this is the first time I've heard of the feedback node method for LV2 globals, the question I have is are there any drawbacks or perhaps other advantages to using it other than speed? And what of the warning dialog that beckerg noticed?

The feedback node is something that was covered way back in the Basics course, but I honestly never really found a practical use for it (yet). This seems like it might be useful, but I want to make sure that there isn't something that I should watch out for.

Thanks!

Link to comment

Well, my little question -- and this groups' response -- has churned up some concern within R&D. I'm passing the feedback back. As far as code inspection goes, there should be no performance difference between the two loops. Regarding the free-range feedback node, opinions are divided about whether there should be any difference there or not ...

Link to comment

QUOTE(Tomi Maila @ Mar 29 2007, 10:38 AM)

I didn't find the old tests but I quickly wrote new ones. They are attached. Feedback node was fastest, while loop second and for loop last.

The feedback method has four buffer allocations inside the outermost for loop. The for method has five and the while method has six. Could that have something to do with it? I'm not sure how constant folding will affect those or exactly what happens when a loop element (i or N) is left unwired.

Link to comment

QUOTE(Ben @ Mar 29 2007, 08:24 PM)

I am not sure (yet) if I trust these tests in the context of an action engine.

That's very wise of you as I made a mistake. The feedback node of mine doesn't work without the outer loop. To work it would need to have a loop.

Jim, how do you create this feedback node of yours? It seems to be inside a hidden while loop. And it seems the hidden while loop is set up so that it goes on forever. At least for me. Did you really create this with LV 8.20 or did you happen to use something non-public?

Tomi

EDIT: The updated feedback node test is attached. It's still fastest but not by so clear marigin.

Link to comment

QUOTE(Tomi Maila @ Mar 29 2007, 12:42 PM)

EDIT: The updated feedback node test is attached. It's still fastest but not by so clear marigin.

There seems to be a constant folding bug with your updated VI. The output is always 0. I think this is a known issue. I don't know if this will affect the speed of operation or not.

You also don't have the same First Call? or case structure as in the other examples. When I add them in, it seems to be quite a bit slower than the other two methods. (The difference between the While and the For methods is only about 1% for me.)

Link to comment

QUOTE(eaolson @ Mar 29 2007, 09:34 PM)

You also don't have the same First Call? or case structure as in the other examples. When I add them in, it seems to be quite a bit slower than the other two methods. (The difference between the While and the For methods is only about 1% for me.)

First call is not needed as feedback node can be initialized in the edge of the loop.

Link to comment

QUOTE(Tomi Maila @ Mar 29 2007, 10:42 AM)

Jim, how do you create this feedback node of yours? It seems to be inside a hidden while loop. And it seems the hidden while loop is set up so that it goes on forever. At least for me. Did you really create this with LV 8.20 or did you happen to use something non-public?

I am under NDA not to say anything until this Sunday.

Link to comment

QUOTE(Tomi Maila @ Mar 29 2007, 01:41 PM)

First call is not needed as feedback node can be initialized in the edge of the loop.

So can a shift register. The initialization happens on each call, which makes an initialized feedback node not so useful for an LV2 global. I was just trying to point out that comparing a feedback-method global without First Call? and a while-method global with it is like comparing apples to oranges.

Execution times, 10^8 iterations: (YMMV)

feedback-method global with initialization only: 1272 ms

feedback-method global with initialization and First Call?: 4693 ms

for-method global: 4600 ms

while-method global: 4200 ms

Link to comment

Stephen, I tried it before a couple of times, but I went back to the while loop.

The main reasons -

  • It's clearer to most developers (including me). Since a for loop's number of iterations can be determined by any number of things wired into the loop, I often wouldn't "see" a small constant wired into the N terminal. While loops have a single termination point and the T constant is commonplace.
  • As Ben suggested, sometimes you would want an unknown number of iterations.

Link to comment

QUOTE(eaolson @ Mar 29 2007, 10:10 PM)

So can a shift register. The initialization happens on each call, which makes an initialized feedback node not so useful for an LV2 global. I was just trying to point out that comparing a feedback-method global without First Call? and a while-method global with it is like comparing apples to oranges.

You must be right. I've not been using feedback nodes so I misunderstood the meaning of initialization node, I guess.

Tomi

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.




×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.