LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Darren's Weekly Nugget 07/20/2009

If you need to conditionally add elements to the beginning of an array, you would probably write code that looks like this:

 

Build_Array_Front.png 

 

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:

 

Build_Array_End_and_Reverse.png

 

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.

Message 1 of 24
(13,006 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
Message 2 of 24
(12,998 Views)
0 Kudos
Message 3 of 24
(12,981 Views)
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.
Message 4 of 24
(12,950 Views)
Awsome tip today. It's usually kinda hard to know what LV is doing under the hood so I always love optimization nuggets.
CLED (2016)
0 Kudos
Message 5 of 24
(12,923 Views)

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.

Message 6 of 24
(12,920 Views)
Nice manupulation. Hmmmmmm things u can do in lv........
0 Kudos
Message 7 of 24
(12,809 Views)
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.
Message 8 of 24
(12,750 Views)

Thanks Darren,

 

Excellent tips as always!

0 Kudos
Message 9 of 24
(12,695 Views)

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

Retired Senior Automation Systems Architect with Data Science Automation LabVIEW Champion Knight of NI and Prepper LinkedIn Profile YouTube Channel
0 Kudos
Message 10 of 24
(12,683 Views)