LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Simple MathScript running very slowly.

Solved!
Go to solution

Howdy all,

 

I have something that appears to be a pretty simple script:

 

if delt ==0
    newarray = datain;
else
   [rows columns] = size(datain);
   timediff = datain(2,1) - datain(1,1);
   totrow = delt / timediff;
   numrun = rows -totrow;
   newarray = zeros(numrun,columns);
   for ii = 1:numrun
      newarray(ii,:) = [datain(ii+totrow,1) mean(datain(ii:ii+totrow,2:columns))];
   end
end
output1 = 1;

 

So the point of the code is to take a rolling time average of some data. Putting the same data in MATLAB takes much less than a second to run, depending on delt (the number of seconds of the average), but can be as low as 5 100ths of a second. In Excel it can calculate it in around 0.5 seconds, but in LabView, it'll take at least 10 seconds and up to about a minute for a long (10 second) rolling average.

 

Why is it so much slower in LabView?

 

Cheers,

James

0 Kudos
Message 1 of 12
(4,519 Views)

 


@JamesVenning wrote:

Why is it so much slower in LabView?

 


 

 

Properly programmed, taking a running average in LabVIEW is extremely effcient.

(You could modify starting from this code, for example)

 

You make two mistakes:

 

 

  • Using mathscript for a simple task like this. It problaby could be done better, so you might ask here.
  • You are thrashing the memory by constanly allocating (resizing) array data.

 

I am not very fluent in matlab code. Still, you might want to try plain g code. What are typical input values?

 

Can you attach your working (slow) mathscript code and some typical default data?

 

 

 

0 Kudos
Message 2 of 12
(4,465 Views)

I thought by using the zeros command I had already preallocated the array, meaning it wouldn't have to resize it or anything.

 

The code I used is in my original post, and some sample data is attached.

 

Thanks.

0 Kudos
Message 3 of 12
(4,453 Views)

 


JamesVenning wrote:

The code I used is in my original post, and some sample data is attached.


 

No, it's not!

 

Please attach the entire Vi containing the mathscript node, including the connectors, typical default values, and how you read the data from the file.

0 Kudos
Message 4 of 12
(4,443 Views)

Sorry, I didn't realize you were after the whole thing...

 

You'll need to take the .txt extensions off the files for them to work... It's just I couldn't upload them without the extensions...

 

Thanks again.

0 Kudos
Message 5 of 12
(4,420 Views)

Shouldn't there be at least one more data file (your program reads up to four)?

0 Kudos
Message 6 of 12
(4,393 Views)
Solution
Accepted by topic author JamesVenning

OK, here's a quick draft of a pure g version. It takes about 60ms on my computer for the averaging of the existing files.

 

(See if it is any faster. I am sure there is quite a bit of slack left and it could be sped up quite a bit with some fine tuning.)

 

Your code has quite a few problems, for example is uses 100% of the CPU when it is not doing anything (place an indicator on the two iteration terminals to see why). I also don't like the stacked while loops. I used an event structure instead. Lots of error handling needs to be added, e.g. if fine 2 is ther but file 1 is missing, for example. You have quite a few output oercions on your mathscript node, for example misrepresenting the dimensionality of the arrays.

 

Anyway, the attached should get you started... 😄 Most likely there are some bugs....

Message 7 of 12
(4,373 Views)

Wow that is awesome. I'm struggling a little to figure out exactly how it happens, but why is it so much faster?!

0 Kudos
Message 8 of 12
(4,360 Views)

 


@JamesVenning wrote:

Wow that is awesome. I'm struggling a little to figure out exactly how it happens, but why is it so much faster?!


 

OK, as I said there is a lot of slack left!! 🙂 Here's a version that does the same in a few milliseconds (>30x faster than my old version).

It just needs to be coded a bit more intelligently...

 

See if you figure out the algorithm... 😉 

 

(HINT: These constant array subset operations read the same element many times in succession, so that's of course a waste! If you do it right, each element needs to be touched only once. :D)

 

(It is currently in 8.2, so make sure to save it in your version before benchmarking)

 

To answer your question: Mathscript does have additional overhead, so if performance is an issue, stick with native graphical code. (Mathscript speed has significantly improved over the versions, so in LabVIEW 2010 it would most likely be a bit faster, but I have not tested it)

Message 9 of 12
(4,355 Views)

Yeah I've spent a bit of time this morning slowly going through how it works. I'm okay with everything up to the bit 'fourth mathscript node', but I'll keep going through trying to figure it out.

 

The first column of the data is the time column, which I wouldn't want to be changed at all, except that the first averaged series should be at the input time, (ie. if a 2 second average, the first data should be 2, then 2.004, then 2.008, etc.) . Should I take it out of the array at the start of the program and add it back in later? Or should I get the correcting and averaging to disregard it?

 

Thanks again.

0 Kudos
Message 10 of 12
(4,349 Views)