Matlab .NET Performance

I have a large set of simulators written in Matlab, but because of concerns about better interaction with other internal projects (as well as speed of execution), I am thinking about porting some functions to .NET and calling such objects from Matlab. What is the overhead associated with calling .NET objects from Matlab?

Here's a nice discussion about Matlab OO that doesn't talk about .NET

Edit : Brief study

I did a quick test myself on Matlab, from simple access and assignment operations on various objects, including the formal Matlab objects (R2011b), Java, and .NET, naming them 1,000,000 times. The method call refers to the inner loop, property / field calls refer to accessing the public field from Matlab and the looping loop in Matlab. The latest results will puzzle me, since the overhead for .NET is much higher than Java, but the actual runtime is about half. What's happening?

  Access (s) Assign (s) Type of object / call
     --- MATLAB ---
     0.003361 0.004268 'myObj.field'
     0.003403 0.004263 'myStruct.field'
     0.003376 0.003392 'myVar'   
     0.152629 0.303579 'myHandleObj.field'
     25.79159 - 'TestConstant.const'
     0.003384 - 'myTestConstant.const' (instance)
     0.006794 0.008689 'TestObj.methods'
     0.157509 0.303357 'TestHandleObj.methods'

     --- NON-MATLAB ---
     10.70006 16.42527 'JavaObj fields'
     0.005063 0.005441 'JavaObj methods'
     43.49988 43.96159 'NetObj fields'
     0.002194 0.002306 'NetObj methods'
+6
source share
2 answers

Significant overhead when working with .NET methods in Matlab.

I did a little test in Matlab (8.0.0.783 (R2012b)):

v = zeros(10000,1); for i=1:3 rnd = System.Random(); tic; for j=1:10000, v(j) = rnd.NextDouble(); end; toc; dt = System.DateTime(2014,1,28,0,0,0); tic; for j=1:10000, dt = dt.AddSeconds(1); end; toc; end 

This applies to Matlab in my PC application. 0.5 seconds for the primary circuit and 1 second for the secondary circuit. In pure .NET code, which takes 0.00015 and 0.0002 seconds. Thus, the overhead of calling a .NET object method in Matlab depends on the object and the .NET method at hand.

For more complex .NET object methods, the overhead can be even worse. I am responsible for the .NET API for accessing files in a specialized scientific data format.

This .NET API can be used from Matlab. In the worst case, you read only one double or float value each time you call the .NET reading method (time series files: for each moment there is one value (double or float) for several elements).

A script to read such a file shows that Matlab on my laptop can make slightly less than 1,000 calls in .NET per second.

The funny thing is that if I put the same code in the Matlab function (basically puts the read_file () function as the first line in the script), Matlab makes about 6.500 .NET calls per second. Thus, inside the Matlab function is about 8 times faster than inside the script when it comes to calling these .NET methods. This does not reproduce with the above test case.

The bottom line is that when calling the .NET method from inside Matlab, a significant overhead occurs. It is important to make the .NET api "chunky" instead of "chatty".

We solved our problems by creating a set of "chank helper" methods in the .NET dll utility that does all the reading, collects data in a large matrix and returns the matrix in a single Matlab call, mainly minimizing activity over the Matlab-NET border.

Suitable .NET code seems to be as fast when starting from a clean .NET application, as when starting from Matlab.

+2
source

A running application consumes resources for recalculating and redrawing data using the GD I / GD I + tools. Both operations are not related to each other. Recalculation means sorting, filtering, grouping, and all other operations except repainting.

0
source

Source: https://habr.com/ru/post/911877/


All Articles