faster Math.exp() via JNI?

前端 未结 15 567
时光取名叫无心
时光取名叫无心 2021-01-11 22:43

I need to calculate Math.exp() from java very frequently, is it possible to get a native version to run faster than java\'s Math.exp()

相关标签:
15条回答
  • 2021-01-11 23:42

    There's simply an overhead associated with using the JNI, see also: http://java.sun.com/docs/books/performance/1st_edition/html/JPNativeCode.fm.html

    So as others have suggested try to collate operations that would involve using the JNI.

    0 讨论(0)
  • 2021-01-11 23:44

    Write your own, tailored to your needs.

    For instance, if all your exponents are of the power of two, you can use bit-shifting. If you work with a limited range or set of values, you can use look-up tables. If you don't need pin-point precision, you use an imprecise, but faster, algorithm.

    0 讨论(0)
  • 2021-01-11 23:46

    +1 to writing your own exp() implementation. That is, if this is really a bottle-neck in your application. If you can deal with a little inaccuracy, there are a number of extremely efficient exponent estimation algorithms out there, some of them dating back centuries. As I understand it, Java's exp() implementation is fairly slow, even for algorithms which must return "exact" results.

    Oh, and don't be afraid to write that exp() implementation in pure-Java. JNI has a lot of overhead, and the JVM is able to optimize bytecode at runtime sometimes even beyond what C/C++ is able to achieve.

    0 讨论(0)
提交回复
热议问题