LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Calculation with MATLAB script is much slower

We are using MATLAB script in LabVIEW.
Calculation with this MATLAB script is much slower than MATLAB alone calculation.
For example, in the case of calculation in which multiplication of an array is repeated,
A = ones (1000)
B = A * A
The time required for one multiplication is about 1.3 s in MATLAB script and about 0.05 s in MATLAB alone.
                                 
Currently I think that this is due to the limitation of CPU usage of MATLAB script.
The CPU utilization at the time of calculation is about 15% at the time of MATLAB script and 60% -70% at MATLAB alone.
I am using LabVIEW 2015 64bit.
Can you tell me if you know the fast calculation method in MATLAB script or the method to cancel the CPU usage restriction?

0 Kudos
Message 1 of 8
(4,485 Views)

Hi nagao,

 

do the multiplication purely in LabVIEW to get rid of the communication overhead between LabVIEW and MATLAB (which needs to be done for the MATLAB script node)!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 2 of 8
(4,451 Views)
Hi,GerdW

What you're talking about is a way to calculate with LabVIEW only? I would like to calculate on the MATLAB script node.
0 Kudos
Message 3 of 8
(4,448 Views)

Hi nagao,

 

when you insist on using the MATLAB node then you have to live with it's speed - no way to influence it.

 

Otherwise you could port your script to LabVIEW to get full speed…

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 4 of 8
(4,445 Views)

@nagao wrote:

We are using MATLAB script in LabVIEW.
Calculation with this MATLAB script is much slower than MATLAB alone calculation.
For example, in the case of calculation in which multiplication of an array is repeated,
A = ones (1000)
B = A * A
The time required for one multiplication is about 1.3 s in MATLAB script and about 0.05 s in MATLAB alone.


The same calculation (in LabVIEW 2016, 32-bit, on Windows 7, 64-bit) took less than 6 milliseconds!  Replacing the first step with generating 1000 random (between 0 and 1) numbers (which is included in the time calculations) again is less than 6 msec in LabVIEW.

 

So if you are going for speed, use LabVIEW (which is compiled, rather than interpreted).

 

Bob Schor

 

Oops -- made a significant error.  I forgot that ones(1000) gives a 1000 x 1000 array, not a 1000 x 1 array.  I'll be back with new numbers shortly ...

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

@Bob_Schor wrote:

@nagao wrote:

We are using MATLAB script in LabVIEW.
Calculation with this MATLAB script is much slower than MATLAB alone calculation.
For example, in the case of calculation in which multiplication of an array is repeated,
A = ones (1000)
B = A * A
The time required for one multiplication is about 1.3 s in MATLAB script and about 0.05 s in MATLAB alone.


The same calculation (in LabVIEW 2016, 32-bit, on Windows 7, 64-bit) took less than 6 milliseconds!  Replacing the first step with generating 1000 random (between 0 and 1) numbers (which is included in the time calculations) again is less than 6 msec in LabVIEW.

 

So if you are going for speed, use LabVIEW (which is compiled, rather than interpreted).

 

Bob Schor

 

Oops -- made a significant error.  I forgot that ones(1000) gives a 1000 x 1000 array, not a 1000 x 1 array.  I'll be back with new numbers shortly ...


OK, now I'm generating a 1000x1000 point array (I tried both all 1's and all random from 0 to 1).  I also didn't count the time for array generation, just the multiplication.  All the numbers are Dbls.  Both all 1's and all random take about 82-86 milliseconds, perhaps slightly slower than Matlab (hmm, let me time this in Matlab on the same PC ...).  

 

So I just fired up MatLab 2014b (64-bit) and ran A = ones(1000); tic; B = A*A; toc, and got 190 milliseconds.  So again, LabVIEW seems to be faster (whew!).

 

Bob Schor

0 Kudos
Message 6 of 8
(4,415 Views)

A note to my previous comments on Matrix Multiplication in LabVIEW -- the problem that was posed was to multiply a 1000x1000 array of all ones by itself.  When I coded this in LabVIEW, I created the array, transformed it into a matrix, multiplied the matrix by itself (by wiring the matrix into both sides of a Multiply function), then transformed it back to a 2D Array.  This took about 85 milliseconds.  I then did the same thing with the "Square" function, and it took only 2 milliseconds!  However, it did not recognize that it was doing a matrix multiplication -- instead of getting an array of all 1000, it got an array of all 1.

 

I'm going to call this to NI's attention -- not sure if this is a "bug" or a "feature", but it is (at least, to me,) unexpected.

 

Bob Schor

0 Kudos
Message 7 of 8
(4,410 Views)

Can you post example how are you calling it?

I am worried about transfer of tons of data between Matlab and LabVIEW. I guess labview and matlab do not share memory  and duplicate data a lot. Matlab does all memory initalization after it receives data, Labview does not know how large array it will receive from Matlab and can not reuse original memory.

Also I do not know how long does it take to initialze Matlab engine.

So if you send single parameter, do complex calculation in Matlab, get single parameter, it will be not the same as splitting these calculations between Matlab and labview.

0 Kudos
Message 8 of 8
(4,402 Views)