07-20-2009 11:27 AM
If you need to conditionally add elements to the beginning of an array, you would probably write code that looks like this:
Did you know that it is very inefficient to use Build Array in this manner? When you add elements to the beginning of an array with Build Array, it can be many times slower (sometimes 100x slower!) than adding elements to the end. This is because LabVIEW has some optimizations for adding elements to the end of an array (where we allocate memory in chunks instead of one element at a time) that we just can't do when adding elements to the beginning of an array (which involves shifting the entire array in memory to make room for the new element at the beginning). So instead of adding to the beginning, you could add to the end and then reverse the array once you're done:
In most cases, the Reverse 1D Array function adds negligible time to the total execution, even if your 1D array is huge. This is because in most cases, the Reverse 1D Array function, instead of actually doing the work of moving all the elements around in the array, can very simply (under the hood) specify that the beginning of this particular array data is now the last element, and add negative offsets to any specified indices (for indexing, adding elements, etc.) for future array operations accordingly. There are some rare cases where this optimization cannot be done, and Reverse 1D Array really does need to shift all the elements of the array around. So make sure you benchmark your array processing code to ensure you are seeing the performance benefits of reversing the array.
07-20-2009 11:34 AM
Darren wrote:... This is because in most cases, the Reverse 1D Array function, instead of actually doing the work of moving all the elements around in the array, can very simply (under the hood) specify that the beginning of this particular array data is now the last element, and add negative offsets to any specified indices (for indexing, adding elements, etc.) for future array operations accordingly. There are some rare cases where this optimization cannot be done, and Reverse 1D Array really does need to shift all the elements of the array around. So make sure you benchmark your array processing code to ensure you are seeing the performance benefits of reversing the array.
Thanks Darren.
I thought I had seen a reverse 1d array run with no time but I did not believe it. This is sorta like LV version of breaking the first law of thermodynamics.
In LabVIEW you can get "something for nothing".
Ben
07-20-2009 11:51 AM
07-20-2009 12:18 PM
07-20-2009 12:56 PM
07-20-2009 12:59 PM
Gleichman wrote:
Interesting tip, but it will only work as shown starting with an empty array. If you started with a non-empty array, you would have to reverse it first.
Right...nevertheless, the initial reversal and the reversal at the end should still have a negligible effect on overall execution time and memory usage.
07-21-2009 02:06 AM
07-21-2009 07:54 AM
07-21-2009 11:26 AM
Thanks Darren,
Excellent tips as always!
07-21-2009 11:37 AM
DFGray wrote:
Note that it is also far more efficient to reshape the array once (or a small number of times) and replace data in existing array locations than it is to append to the array. In the example above, where the final number of elements is not known, you can reshape occasionally when you run out of array elements and do a final reshape to the final, known size. This is still much faster than iteratively adding to an array and fragments your memory much less.
IT wil take me some time to process that tid-bit but I think there is some hefty meat in there somewhere.
Thank you Dr Gray!
Ben