Search code examples
arrayslabview

Most efficient way to add a constant to a column of an array in LabVIEW?


I want to add a constant to the second column of an array.

I do this as shown below:

enter image description here

Where for illustration the values are as follows:

enter image description here

What is the most efficient way of adding a constant to an array column?


Solution

  • With a question about efficiency you should supply number. For anything lower than a 1000 x 1000 2D array I can't measure the difference. Usually it is best to simply test it.

    Here the code for testing (same answer as crossrulz)

    enter image description here

    With a 10000 x 10000 array option 2 becomes about 10 times faster.

    One comment unless you are in a very high demanding situation, readability is usually preferred over efficiency. In my opinion option 2 is more readable since it has no for loop and the constant is presented as a constant instead of an array.

    But you can get more efficient than that by using the In Place Element structure. The image below shows two different ways to add 5 to a column. The second one avoids making a memory copy of the entire array. Indexing out a column of an array with Index Array and then modifying it requires a shift of underlying memory format, even though the array is going to be put back in the Replace Array Subset. The In Place Element structure gives enough context to LabVIEW for it to recognize that the Add can be done without data copies.
    Add 5 to a column of an array, in 2 different ways