LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Efficient way to build arrays

HI DCKAN,

 

when time matters you should use autoindeing loops and/or arrays of constant size with ReplaceArraySubset.

 

The BuildArray function isn't fast nor efficient!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 11 of 18
(2,079 Views)

@James.M wrote:

@Hooovahh wrote:

Yeah I've heard it is actually faster to perform a reverse 1D array, add at the end, reverse 1D array again, rather than add at the front.  I personally haven't tested this, and if this were the case I'm not sure why the compiler wouldn't just do it this way behind the scenes.


 Gosh, this whole time I've tried so hard to stay away from the Reverse 1D function because I assumed it had more overhead than just creating the array the right way from the start. I guess I'm reminded again what assuming does.

 

Edit: It looks like this is not the case... I get 125x iterations with the above code, but with the Reverse 1D Array nodes, it slows down considerably to where the first method is anywhere from 150x to 280x faster.

Example_VI_BD.png


 

 

Hi James,

 

Why to have reverse array in every iteration???

 

Just put it at very last when array has been build.. I am sure this will fasten code then that you tested before..

 


Best Regards,
DCKAN

"We make a Living by what we get. We make a Life by what we give."
0 Kudos
Message 12 of 18
(2,077 Views)

Oh, I had read Hoover's " I've heard it is actually faster to perform a reverse 1D array, add at the end, reverse 1D array again" as faster at each iteration. Doing it once at beginning and once at the end would obviously be faster, but usually when I'm adding a value at the beginning of the array, it's a one-time thing instead of 100,000 times in a row.

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


0 Kudos
Message 13 of 18
(2,067 Views)

James, don't you want to be comparing adding an element to the start of an array to your reverse array method?  Right now I think you're comparing adding elements to the end of an array to the reverse 1D array method.

0 Kudos
Message 14 of 18
(2,055 Views)

DCKAN wrote:

Even if we know array size very before we build, instead of pre allocating space for array, I would prefer this function if only build is required.


That is precisely the situation for a FOR loop with Autoindexing tunnels.  This is, by far, the most efficient way to build your arrays, time and memory.

 

Plus these benchmark code are completely off.  You should be running a set number of loops and measure the time difference.  If I find some time, I'll try to set up a better benchmark.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 15 of 18
(2,046 Views)

@crossrulz wrote:
Plus these benchmark code are completely off.  You should be running a set number of loops and measure the time difference.  If I find some time, I'll try to set up a better benchmark.

Example_VI_BD.png

Cheers


--------,       Unofficial Forum Rules and Guidelines                                           ,--------

          '---   >The shortest distance between two nodes is a straight wire>   ---'


Message 16 of 18
(2,019 Views)

Here is my benchmark. Array size = 100000. While loop prepend = ~3 seconds. While loop append = ~0.3 seconds. For loop = 1.3 milliseconds. Reverse Array = ~35 nanoseconds.

 

Lynn

 

Build Array.png

0 Kudos
Message 17 of 18
(2,010 Views)

@James.M wrote:

Oh, I had read Hoover's " I've heard it is actually faster to perform a reverse 1D array, add at the end, reverse 1D array again" as faster at each iteration. Doing it once at beginning and once at the end would obviously be faster, but usually when I'm adding a value at the beginning of the array, it's a one-time thing instead of 100,000 times in a row.


Yeah I get your point, and I see your code, it does seem to controdict what I've heard about the reverse 1D array, not sure why.  Someone from NI would need to chime in for an explaination.

Message 18 of 18
(1,987 Views)