JNI 是提高位操作性能的解决方案吗?
我有一个 java 类,它执行非常繁重的位解码和操作。这个类几乎占我应用程序总执行时间的 96%。我想知道如果我为这些位操作编写一些c代码并使用java本机方法加载c库,我的性能是否会得到显着提高?
I have a java class that does really heavy bit decoding and manipulation. This class accounts to almost 96% of my total app's execution time. I was wondering whether if I write some c code for these bit manipulation and use java native methods to load the c libraries, will I get significant increase in my performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
找到答案的唯一真正方法是使用专门构建的 Java 基准测试工具进行仔细的基准测试 - Caliper 来介意——但我个人怀疑您是否会获得足够的性能来超过 JNI 开销。
The only real way to find out is to do careful benchmarking using a purpose-built Java benchmarking tool -- Caliper comes to mind -- but I personally doubt that you'd win enough performance to outweigh the JNI overhead.
正如路易斯所说,最好的了解方法就是测量。通过 JNI 调用的开销很大。更好的问题是您是否经常调用您的方法。换句话说,为了消耗 96% 的执行时间,您的位操作方法是否每秒被调用数千次,或者是否在单次调用中完成大量工作?如果是后者,那么您很可能会从本机代码中受益。
As mentioned by Louis, the best way to know is to measure. The overhead of calling through JNI is significant. The better question to ask is if you are calling your methods frequently. In other words, to consume 96% of the execution time, does your bit manipulation method get called thousands of times per second or does it do significant work in a single call? If it's the latter, then it's highly likely that you will benefit from native code.
我看到 JNI/本机代码更快的唯一方法是,您可以通过将大型数据集传递给本机方法以进行位操作来克服 JNI 调用开销。请注意,与 Java 方法不同,JNI 方法无法通过热点内联/优化。本机调用需要在 Java 和本机内存区域之间来回复制值。
The only way I could see JNI/native code being faster is you can overcome the JNI call overhead by passing a large dataset to the native method for it to bit manipulate. Note that unlike Java methods, JNI methods cannot be inlined/optimized by hotspot. Native calls require copying values back and forth between the Java and Native memory areas.