09-06-2013 01:31 PM
@df86 wrote:
It's better to initialize and then chop off the extra. When you initialize the array that's one copy being made, from there by using replace subset and you will modify the array in the same memory location. I believe array subset will dublicate the array in memory also but this is still more efficient. When you use Build Array, it creates a copy of the data every time, so basically you get the factorial affect. With amount of memory available now a days and a small array size, these advantages could be negligible. But if you start working with arrays with few thousand cells, on hardware that is resource restricted, this will make a significant difference.
As far as test files with only one sample, as long as there is a _# at the end it should work fine.
Hope this helped,
Thanks, df86 - I thought about the factorial effect, too. I wanted to make sure my thinking was sound. In small arrays, the memory usage wouldn't be noticable, but in huge arrays it could be the difference between life and death.
09-06-2013 01:47 PM - edited 09-06-2013 01:50 PM
Ugh, I didn't test it well enough. Since it's alphabetically sorted (strings) it won't sort correctly when iteration numbers have mixed amount of places.
[edit]
i hate it when leading zeroes aren't used in a naming convention...
09-06-2013 07:02 PM
Just so I can make sure you are clear and I'm not miss guiding you, Using build array will not exactly require a factorial amount of memory, (conceptually I used it to clarify the point but it doesn't exactly work that way). Memory Manager in labview is fairly robust. It will attempt to resize the array in place as much as possible, and also in the back ground it will "garbage collect" the memory not being utilize any longer. This comes with a noticable overhead, specially in a complicated sofware which memory is being allocated in different locations in parallel and it could also cause fragmentation which will really cause a performance hit each time the memory manager is called. So the answers remains the same, Yes as much as possible use Initialized array or for loops for creating an array instead of build array.
Dan,
09-07-2013 07:21 AM - edited 09-07-2013 07:22 AM
@billko wrote:
I have a question for the LabVIEW gurus. Is it better to init an array as I did, based upon the size of the original aray (since I didn't know what the final size was going to be, but it couldn't be bigger than the original) using subst array element and chopping off the extra elements, or not init the array and make a bunch of copies of data inside, using a build array?
Some versions ago it was alot more effective to preallocate. Recent versions are basically equal, in which case i prefer to use Build array for simpler code. The build array probably uses the same logic as Java, a new array allocates e.g. 100 bytes, and if needed it doubles allocated memory, until you run out ot memory or is finished. However, if it happens in a for loop, it'd be pretty clever of the compiler to preallocate the number of loops to begin with, right? 😉
So there doens't seem to be much data copying nowadays due to good compiler optimization, so write the code that's easiest to read and maintain. 🙂
/Y
09-09-2013 12:21 PM
@Yamaeda wrote:
Some versions ago it was alot more effective to preallocate. Recent versions are basically equal, in which case i prefer to use Build array for simpler code. The build array probably uses the same logic as Java, a new array allocates e.g. 100 bytes, and if needed it doubles allocated memory, until you run out ot memory or is finished. However, if it happens in a for loop, it'd be pretty clever of the compiler to preallocate the number of loops to begin with, right? 😉
So there doens't seem to be much data copying nowadays due to good compiler optimization, so write the code that's easiest to read and maintain. 🙂
/Y
Based on the white papers I've read from NI, you are actually incorrect. The build array is no where close to efficiency of the prebuilding an array. And also if you use an INDEXING tunnel in a for loop, it does preallocate the memory required for building the array. If you use indexing in a while loop, since it doesn't know the size of the array it is not as efficient but as it needs more memory space it will enlarge the size of the allocation significantly therefore it will not require to reallocated memory on every iteration of the while loop. On the other hand Build array is by far the worst way to create an array.
I have two VI's attached. Each will create two arrays, look at the cycle time. And yes, using a nonreal time operating system doesn't give you an accurate cycle period but since these cycle times are order of magnitute apart from one another we can ignore the few ms jitter. If you like to really make it interesting based on the capability of your computer adjust the Array size so the buld array takes a few seconds to complete. Then open on Task manager and look at labview memory consuptions.