English 中文(简体)
是否值得从Matlab打给Java?
原标题:Is it worth to call java from matlab?

The reason behind the question

对于物理工程, 我们要分析两个表面( 由一堆顶点指定) 并计算这些表面之间的体积。 要做到这一点, 我们想要执行一个二维间距搜索树 。 在总计的无症状运行时间中, 将会接近 O( n 2 log n ) 。

The question

简言之,我们将实施一种算法,这个算法将花费相当多时间。 此外,算法无法从高优化的马特拉布数学图书馆获益。 因此,我们正在考虑从马特拉布调用爪哇。 我们想知道:

"爪哇在什么程度上比长板更快, 忽略了数学图书馆在长板上的高度优化表现?"

"在从马特拉布反复呼唤java方法时,有没有出现任何重大延误?"

例如,每次调用该方法时, JVM 是否必须启动? 是否每次调用该方法时, 都要装入罐子?

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

最佳回答

若您决定在 MATLAB 中写下执行文件, 以下是一些非常好的要点: < a href="https://stackoverflow.com/a/1745686/97160" ,

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.

为了回答你的问题,我引用文件:

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.

所以在启动时它只初始化一次 。

与M文件相比,在调用Java方法时也有间接费用(因为MATLAB类型必须从Java数据类型中排出)。

现在,如果你想挤出所有最后一点的表演, 一定要称 Java 方法为:

func(obj)

取代:

obj.func()
问题回答

我曾经在 MATLAB 命令行内与 Java 类合作过好几次。 我不知道每次你给 Java 打电话时 JVM 是否都重新启动, 我也没有测量过执行独立 Java 类的延迟程度。 然而, 我还没有“ 触动” 任何慢速 。

看来您需要为您的问题创建自定义的数据结构, 这对于 MATLAB 是您无法轻易或高效地做的。 另一个有助于决定使用谁的问题就是这个问题: 使用 Java 或使用 Matlab 的开发会更快吗? 如果答案是 Java, 我绝对建议与 Java 一起使用 。

这个问题似乎非常适合平行主义。 为什么爪哇是你首先考虑的唯一选择? 我认为你应该看看最初代码的性能如何,如果有必要,你应该研究一下使用一些 MATLAB 内置的 GPU 功能。

我建议你尝试执行一个在你看来最简单的方式的原型,看看它的表现如何。如果表现显然不够好,请尝试另一种方法,看看它是否更好。

(可能很难对这种情况作一般预测,因为真正的答案往往关键取决于问题的细节和设计。 另一方面,你可能发现,你没有以所谓效率更高的方法取得足够的加速,来证明这种努力的合理性。 )





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签