09-02-2012 06:46 PM
Hello all,
I'm sure this is a quick one, but I'm missing the concept.
I have an array of boolean in a shift register. I have a case where I need to basically 'reset' the array rather than append to it....what is the best way to do this?
Thank you,
cayenne
Solved! Go to Solution.
09-02-2012 07:11 PM
Use an empty boolean array constant to clear your array.
steve
09-02-2012 09:45 PM - edited 09-02-2012 09:46 PM
Except for your word "append", it is not really clear what you mean by "clear" an array". It seems you want to reset the size to zero elements.
(Efficient and well written programs typically keep fixed-sized array in shift registers, so a reset could also mean set all elements to FALSE.)
Here's code for both interpretations. 😄
09-03-2012 05:43 AM
Christian,
I was curious about the relative efficiency of using Reshape Array vs. an Array Constant to resize an array to zero so I made a benchmark program. In the process, I stumbled onto something unexpected. It appears that using Reshape Array with a dimension size of zero is significantly slower than when specifying a dimension size of 1 or more.
Testing with an Array Constant used 4 blocks of memory vs. 6 for the Reshape Array version.
LV2010 SP1
Of course, it is possible that my benchmark program is flawed.
steve
09-03-2012 12:00 PM - edited 09-03-2012 12:03 PM
@stevem181 wrote:
Of course, it is possible that my benchmark program is flawed.
Yup, disable debugging and you will see that the inner FOR loop is folded in both cases and the time drops to about zero.
Testing with debugging enabled is potentially unfair, because the reshaping will have more debugging code because there are more potential places to probe a wire, for example. All that said, I don't really know what's more efficient. That would require much more sophisticated testing. My point was just a reminder that there are always many ways to do things.
(This is under LabVIEW 2012. Constant folding and dead code elimination of course differ between compiler versions)
Granted, I typically don't use any of these two versions: I also omit the diagram constant, set the tunnel to "use default if unwired" and keep in unwired in the "clear" case. 🙂 Less diagram clutter and quicker coding. 😄
Here's how it would look like.
09-03-2012 12:28 PM - edited 09-03-2012 01:16 PM
Trying to prevent folding on the inner loop, I came up with the following code. I don't know if it is sound, but things remain really fast (debugging disabled), but folding no longer shows.
You need to set the iteration on the outer loop to 100M (1e8) to even see a measurable delay. I see the following times:
75ms reshape
65ms diagram constant
65ms unwired and use default
This suggests that we don't have to worry about performance here. Clearly, the compiler still does some magic which I don't fully understand. For example if you would wire the array across unchanged on both cases of the case structure, the code slows down by a factor of about 30x. Why?? (Yes, the SR and output tunnel carries more data but ...)
I stick to my statement that "unwired&use default" is the cleanest option.