06-17-2012 09:18 PM
Hello,
I've been working on a program that scans images pixel by pixel and I was doing this with a double for-loop because this is how I would normally go about this. I created a double for-loop in both a LabView VI and in a MATLAB script call. I thought that since the LabView VI can do parallel processes it would run faster than the MATLAB script which is serial. I found our that the MATLAB script call actually runs about 1800 time faster which got me wondering...
What is the fastest external code that Labview can work with?
06-18-2012 06:26 AM
Sounds strange, LV shouldn't be much slower, if at all. Can you post the code? Did you setup the loops for parallell processing? (they're serial per default)
/Y
06-18-2012 12:09 PM
Here's a screenshot of my code. I tried to make it as parallel as possible but the problem itself is quite serial in nature. I welcome any suggestions
06-18-2012 12:33 PM
It's often more helpful to attach your VI, not just an image of it.
Does the code you posted produce the expected results? It looks to me like you should be using shift registers in the for loop instead of tunnels for the arrays. This will probably speed up the loops, and give you better results. From what I can see in that image, if the false case is normally executed, you're replacing an array element on each loop cycle and then discarding the modified array on the next loop iteration, which is both inefficient and probably algorithmically incorrect.
06-18-2012 12:38 PM
Also, I have you compared the timing of the execution of just the loops? Your performance hit may be one of the other parts of your code in the subVIs.
06-18-2012 12:53 PM
As someone already mentioned this code cannot produce the correct results as it lacks shift registers. However, it is even quicker to use auto-indexing. The way your code works there is no reason not to use auto-indexing. Post the VI and we can optimize it for you.
06-18-2012 01:11 PM - edited 06-19-2012 01:33 PM
Well, your question was not how to improve the current LabVIEW code (and yes, there is probably quite a bit of slack left! ;)), but why it is orders of magnitude faster than Matlab.
You have not shown us the matlab script version, so we can only guess.
06-18-2012 03:16 PM
Ok so I isolated just the double for-loop and MATLAB peices of the my code (below) and made them logically equivalent (using the same processes at the same times in each algorithm). The performance difference dropped drastically, which may be because of my high proficiency in MATLAB and my low proficiency in Labview, to about a difference of 4 times (0.465 sec in MATLAB 1.785 sec in Labview). The MATLAB code generates the result I'm looking for but the LabView code does not which maybe because of me not using shift registers (I'm new to this so I need to look this up and learn to implement it which I will be doing in the mean time).
As per altenbach's request computation data is...
MATLAB LabView
10-28% CPU usage 12-16% CPU Usage
2.08 GB memory usage 1.785 GB memory usage
0.465 sec to complete 1.785 sec to complete
I left the method I used to get the times in the examples.
Thank you for any insight you can provide to making this difference shift in favor of LabView
06-18-2012 03:31 PM
Without looking at your MATLAB code at all... try this simple change to your LabVIEW code: change the tunnels (the blue boxes on the border of the for loops where the arrays enter and exit the for loops) to shift registers. Right-click on each incoming tunnel, choose "Replace with Shift Register" and then click the corresponding tunnel on the outgoing side of the for loop (matching inputs to outputs).
You might also want to use the "In Range and Coerce" function instead of separate Greater/Less Than comparisons, and you could change the numeric representation of the 0 constant in the case structure to a U8 to match the type of data in the array (right-click, Representation).
06-18-2012 04:29 PM
Your algorithm in LV is not the same as in MATLAB. You need a square root in the calculation of hyp.
Can you post some typical data arrays for those of us who do not have IMAQ? Also the expected result so we can check things.
You definitely need shift regsiters. I think you can use autoindexing although a transposition may be needed. Altenbach will shortly remind us that suing complex math may simplify things, also.
Lynn