LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Is insert into array function not desirable in programming?

Hi All,
 
I am working with LabVIEW by one year.
 
I have developed many database oriented applications which deals with a vast amount of data in access database.
I noticed that some of the applications were taking some minutes for execution(for example,plotting data of 5 years...)
I thought it is because of the large amount of data that has to be handled.
 
But recently when i was going through posts in labview board,i noticed that it is not desirable to use insert into array function so frequently.
I remember i used the funtion in my applications.The usage of this function will make the application so slow?
 
And i read that its better to use replace array subset , after initialising the array to some values(so some kind  of static memory allocation?).
I remember in C,C++... its better to use dynamic memory allocation than allocating memory statically.
 
Then one more thing what is the importance of wait inside a loop?
A loop without a 'wait' will perform badly than a loop with 'wait'?
 
Thanks in advance
 
0 Kudos
Message 1 of 9
(3,807 Views)
Hi user_1,
 
replacing array elements is faster because you don't need to reallocate memory with every "insert into array". Each insert will grow the array demanding for more memory space: allocating new memory, copy old data & new element, deallocating old memory space... I C you should have similar behaviour with dynamic memory handling...
 
The "wait inside a loop" is necessary for user interface handling: when you don't include a wait statement the loop runs as fast as possible (for "proper" calculations this should be no problem) hogging all the processor time - your program will/can react slowly to user input!
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 2 of 9
(3,802 Views)
Whenever you use "insert into array", a new copy of the array needs to be made in memory because the array size changes. This is OK if it only happens at rare ocasions but becomes very expensive if you do it over and over in a tight loop. Operations are most efficient if the can be done "in place". Often the array is kept in a shift registers aond modified via "replace array subset"


user_1 wrote:
Then one more thing what is the importance of wait inside a loop?
A loop without a 'wait' will perform badly than a loop with 'wait'?

You need to distiguish between "polling loops" and "computational loops".
 
A polling loop typically is not really doing anything except serving the UI (reading controls) and recalculating things. You definitely don't need to recalculate more than 10x a second or slower. If you don't place a wait primitive, the computer will spin the loop as fast as the CPU allows (millions of times/second) while not really doing anything useful. It is just recalculationg the same thing over and over again. A wait ensures that other programs running on the computer don't get starved for CPU.
 
Most often, an event structure is a better choice, because the result only needs to be recalculated whenver an input changes.
 
A computational loop should never have a wait. These are inner loops that carry out some finite computational iteration, iterating over an array in a FOR loop, doing a newton approximation in a while loop, etc.
 
A good execise would be to wire a few alternative algorithms and see how they perform on typical data. Good code can be several orders of magnitude faster than bad code.
 
If you have a very specific algorithm in mind, let us know and we'll tell you what the most efficient way would be to do things.
 
If you have LabVIEW 8.5, you might also want to look at the new "memory control" tools such as the "in place element structure".
Message 3 of 9
(3,796 Views)

Hi GerdW,

Thanks for the informative reply.

I have one more doubt.

We have to initialise the array with some value in the beginning.We can not predict the size of the array in the beginning.

Suppose we initialised the array with few elements and in a later step i have to deal with more elements for that array.So when i use replace function i will have to replace some elements in the index which was unused by the array.(Suppose the initial size is 5 and i am dealing with 100 elements.We can index from 0 to 4 only as there are only 5 elements in the initialised array)

Will this function work in such a situation?

Thanks once again.

 

 

 

0 Kudos
Message 4 of 9
(3,794 Views)
Hi user_1,
 
if you initialize an array with 5 elements and later you need an array size of 100 elements you can reshape it to the new size. All old values stay equal.
After reshape you can replace the elements with an index greater than 4.
 
Mike


Message Edited by MikeS81 on 01-16-2008 04:00 AM
Message 5 of 9
(3,788 Views)


user_1 wrote:

Suppose we initialised the array with few elements and in a later step i have to deal with more elements for that array.So when i use replace function i will have to replace some elements in the index which was unused by the array.(Suppose the initial size is 5 and i am dealing with 100 elements.We can index from 0 to 4 only as there are only 5 elements in the initialised array)


Arrays with 5 or 100 elements are very tiny and it's not even worth bothering about performance. You can safely allocate a few hundred elements and it will not hurt your memory footprint in any noticeable way. Just pick a reasonable upper limit. A typical computer has probably about 1 GB of RAM, so these 100 elements (e.g. 800bytes of DBL) occupy only a microscopic fraction of your total memory.
Message 6 of 9
(3,781 Views)
You can use show buffer allocations to find places where a copy of data is made. If this happens for large arrays, try to eliminate these for better performance.

Felix
Message 7 of 9
(3,774 Views)

Hi Altenbach,

Thank u for the reply.

I am not dealing with array arrayof small size.My arrays need to occupy many thousand of records.(so how do i initialise the array in such a condition?)

Just for an example i asked like Arrays with 5 or 100 .

Thanks once again...

 

 

 

0 Kudos
Message 8 of 9
(3,734 Views)
Hi user_1,

there's an "init array" function in the array palette!

When the array(s) get too big (talking about hundreds of MB) you need to be very careful to avoid creating data copies - use the "show buffer allocations" tool as said above. When using even bigger datasets you need to use files instead of keeping all data in memory...
Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
Message 9 of 9
(3,708 Views)