LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Code optimization

Can anyone please tell me how to optimize this section in order to make it run faster? It gets extreamly slow with the increase in array size.
Thanks in advance!
0 Kudos
Message 1 of 7
(3,270 Views)

Hi Salac,

I am not very sure about the speed of the code, but for better speed and optimization, you can do some thing like type conversion. For example in your code while you are indexing the array, you can convert the value from the iterator "i" to double and so on.

I am attaching the code that you have put with minor modifications, hope this helps.

 

 

0 Kudos
Message 2 of 7
(3,253 Views)
The problem is the "Build Array" function. Dynamically building an array like this is always to cause memory problems if the array gets large. What's going on is since the memory manager has no way of knowing how big this array is going to need to be, the first time it runs accross the Build Array function, it grabs a chunk of memory to place the arra in. It uses this memory chunk until it's full, then it has to grab another chunk, and then another and another.... This is where the slow down come from as the array builds.

I went through this same discussion a few times, and I've posted an example on how to avoid this problem in this reply. That entire thread explains a bit more what's going on and what the example is doing. The example in my last post on that thread uses a 2D array, but it will work just as well with a 1D array.

Repost here if you have any other questions.

Ed


Ed Dickens - Certified LabVIEW Architect - DISTek Integration, Inc. - NI Certified Alliance Partner
Using the Abort button to stop your VI is like using a tree to stop your car. It works, but there may be consequences.
Message 3 of 7
(3,248 Views)
First of all, you need a FOR loop, not a while loop. Now you can use autoindexing on the two input arrays, eliminating the index array functions and the comparison operation. The "# found" is not even needed! ;).
 
Your problem is that the final array size is not known before your loop starts....
 
...BUT you can calculate it by calculating it in a seperate loop beforehand! NOW you can initialize the final array size and use "replace array element" as shown.
 
(Attached is a simple example, please verify operation with some real data. It might need a tweak or two. ;))
 
How big do your arrays get?
Message 4 of 7
(3,226 Views)
Thanks! I've seen that the for loop better fits in this case, but I don't think it's the core of the problem. In this case I could make some arrangements and have the size of the result array be the same as the size of the orriginal array. Should I use the orriginal array and only change it's members? I think that should work. I'll go try that now... will be back with results.
 
 
0 Kudos
Message 5 of 7
(3,208 Views)
It worked!!! Not just worked... it worked very fast!
 
Thank you all for your support. If you guys ever come to Serbia I'd be more than happy to thank You with a drink or something
Smiley Happy.
0 Kudos
Message 6 of 7
(3,198 Views)

Now that you know how to write faster code, try the Prime factor coding challenge ! 🙂

Message 7 of 7
(3,180 Views)