3

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(n2log 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?"

e.g. 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.

Community
  • 1
  • 1
user23127
  • 827
  • 10
  • 21
  • The MATLAB graphical user interface runs within a JVM, so I think it just uses that JVM to execute calls. I don't think it is slow at all, probably comparable with MATLAB calls. I would base my decision on which language coding is easier in. MATLAB has higher level functions than Java for matrix manipulation. – Ansari May 27 '12 at 15:47

4 Answers4

2

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.

emrea
  • 1,335
  • 9
  • 18
1

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.

Adam27X
  • 889
  • 1
  • 7
  • 16
  • We are only considering Java because I develop my part on Mac OSX, some others work on Windows, so we want our program to be as cross-platform as possible. Furthermore, I only know the C++ as other major programming language, and I don't have good experience with C++ and mac. I didn't thought of GPGPU computing, which sounds like a great idea (and opportunity to learn how to do this :) ) – user23127 May 28 '12 at 08:25
1

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()
Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
0

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.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216