From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

philosophical question on two ways to get array size(s) and how compiler optimizes it

For various reasons, I seem to semi-frequently find myself in a situation where I wish to know the size of an array that I also HAVE to (auto-)index through the whole array, and I don't need to know the size until after I'm done iterating... (So not a general case of "I just need the array size".)

 

There are, I'm sure, many ways to get the size but the two most obvious are:

 

A) Array Size primitive  (adds an extra primitive on the block diagram and it introduces a branch on an array wire.. both seemingly con's though both are probably always optimised out by compiler...)

B) Wire the "N" terminal from inside for-loop to outside of for loop with "last value" (adds a terminal to the for-loop (per dimension), and even with "last value" on the output terminal, its not immidiately apparant what optimizations happen when compiled, but it seems this could be made fairly optimized...)

 

Both the above methods are (likely to be) cheap operations, and the compiler should be able to optimize both cases to avoid data copies etc., however "likely" and "should" is as determined as I can seem to get. 

I'm not sure how to benchmark something like this, and there are a lot of potentially convoluted situational modifiers (what if the array contains huge clusters as its elements, what if its a x-D array).

 

So what I'm wondering about, from a purely philosophical point of view as practical implications seem to be nil, is this:

  • Is one method universally better than the other, even if by just a theoretical tick of the CPU?
  • Are both methods universally identical in all cases after compiling?
  • Will it simply come down to a "wiring looks" personal preference?
  • What about multi-dimensional cases (assuming number of dimensions are known when the code is written)
    • What if you only need to know one or two of the dimension sizes (you could then only wire out the N-terminals you need, while "array size" would return an array of all the dimension sizes...)?

 

Example FP and BD:

Block DiagramFront Panel

 

 

Again, this is a philosophical / theoretical question and I'm not claiming any significant performance differences one way or another.. just curious if one way is theoretically better once the compiler is done with the code.. 🙂

QFang
-------------
CLD LabVIEW 7.1 to 2016
0 Kudos
Message 1 of 2
(2,135 Views)

In theory, the compiler should be smart enough to make these operations the same. Personally, I prefer just using Array Size.  It is immediately obvious to newbies what is happening.  The Array Size will not cause a data copy of the array (based on what I have been told by Stephen Loftus-Mercer).

 

But let me throw another wrinkle in there.  What if you have the Conditional Terminal turned on in the FOR loop?  Yes, I run into this a lot due to error handling.  Do you want the size of the original array or the final array?  So now the fight is between the Array Size primitive and the i terminal with an increment.  I imagine the Array Size would be the better way in that case.


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 2 of 2
(2,123 Views)