09-04-2014 12:16 PM
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:
Example FP and BD:
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.. 🙂
09-04-2014 12:55 PM
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.