简体   繁体   中英

Is it worth to call java from matlab?

The reason behind the question

For a physics-project, we want to analyze two surfaces (specified by a bunch of vertices) and calculate the volume between those surfaces. To be able to do this, we want to implement a two-dimensional interval-search tree. In total the asymptotic run time, will be close to O(n 2 log n).

The question

In summary we are going to implement an algorithm, which will be quite time-expensive. Furthermore, the algorithm doesn't profit from the highly optimized math libraries in matlab. Therefore, we are considering to call Java from matlab. Conclusively we would like to know:

"To what degree is Java faster than matlab, ignoring the highly optimized performance from the math libraries in matlab?"

and

"Is there any significant delay when repeatedly calling a java method from matlab?"

eg does the JVM have to be started every time the method is called? does the jar have to be loaded every single time the method is called?

I hope some of you could help me (and maybe others) with this question.

I have worked with Java classes from within the MATLAB command line several times before. I don't know if the JVM is being restarted every time you make a Java call, nor I haven't measured how much the delay is compared to executing a standalone Java class. However, I haven't "felt" any slowness at all.

It seems that you will need to create custom data structures for your problem, which is something you cannot easily or efficiently do with MATLAB. Another question that will help decide which to use is this: will development be faster using Java or using Matlab? If the answer to this is Java, I would definitely suggest going with Java.

This problem seems very well-suited to parallelism. Why is Java the only alternative that you're considering in the first place? I think you should see how the performance is for your initial code and if necessary look into using some of MATLAB's built-in GPU features.

If you decide to write the implementation in MATLAB, here are some very good points @AndrewJanke made in an excellent answer regarding OOP performance in MATLAB (worth reading the whole post):

Mimicking a C++ or Java class in MATLAB probably won't be optimal. Java/C++ classes are typically built such that objects are the smallest building blocks, as specific as you can (that is, lots of different classes), and you compose them in arrays, collection objects, etc, and iterate over them with loops. To make fast MATLAB classes, turn that approach inside out. Have larger classes whose fields are arrays, and call vectorized methods on those arrays.

The point is to arrange your code to play to the strengths of the language - array handling, vectorized math - and avoid the weak spots.

To answer your questions, and I quote the documentation:

At MATLAB startup, part of the MATLAB virtual address space is reserved by the Java Virtual Machine (JVM) and cannot be used for storing MATLAB arrays.

so it is only initialized once at startup.

Also there is an overhead when calling Java methods as opposed to M-files (since MATLAB types have to be marshalled to and from Java data types).

Now if you want to squeeze out every last bit of performance, make sure to call Java methods as:

func(obj)

instead of:

obj.func()

I recommend that you try implementing a prototype the way that seems simplest to you, and see how well it performs. If the performance is clearly not good enough, try the alternative approach to see if it is better.

(It can be hard to make general predictions about things like this because the real answer is often critically dependent on the on the details of the problem and your design. On the other hand, you may find that you don't get enough speedup with the supposedly more efficient approach to justify the effort.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM