LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to save number into array one by one?


@Yamaeda wrote:

My take. 🙂 



This is basically Pachi's solution with "insert into array" replaced by the more natural choice of "build array" and a few other minor changes. (I will never understand why people use "insert into array" in these situations!).

 

You definitely should initialize the middle shift register, else the code could randomly fail if the first element is the same as the last element of the previous run. It could happen!

 

The main problem with these two solutions is performance, because they randomly grow two potentially large arrays in shift registers one element at a time. While the newer versions of the compiler can do some optimizations to avoid constant memory reallocations, the problem still exists.

 

For small arrays of a few thousand elements it won't make a difference, but if you were dealing with huge data structures, the difference can be dramatic.

 

My version could be further optimized, but even in its current form avoids any incremental memory allocations and works on fixed size arrays at all steps. In the first loop it re-uses the input array, replacing elements as needed (Yes we do too many replace operations, but we avoid case decisions, maybe a tossup. Precise benchmarking would need to be done to further optimize. There are a couple of tweaks left that slightly complicate the code but would avoid another buffer allocation). The second loop can allocate the entire 2D output array (or two 1D arrays if desired) when the loop starts and efficiently fill it with a thight loop. Also a very efficient operation.

 

I don't have time at the moment, but feel free to do a few benchmarks. The results might surprise. 😉

0 Kudos
Message 21 of 21
(335 Views)