faster Math.exp() via JNI?

前端 未结 15 566
时光取名叫无心
时光取名叫无心 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:21

    Commons Math3 ships with an optimized version: FastMath.exp(double x). It did speed up my code significantly.

    Fabien ran some tests and found out that it was almost twice as fast as Math.exp():

     0.75s for Math.exp     sum=1.7182816693332244E7
     0.40s for FastMath.exp sum=1.7182816693332244E7
    

    Here is the javadoc:

    Computes exp(x), function result is nearly rounded. It will be correctly rounded to the theoretical value for 99.9% of input values, otherwise it will have a 1 UPL error.

    Method:

        Lookup intVal = exp(int(x))
        Lookup fracVal = exp(int(x-int(x) / 1024.0) * 1024.0 );
        Compute z as the exponential of the remaining bits by a polynomial minus one
        exp(x) = intVal * fracVal * (1 + z)
    

    Accuracy: Calculation is done with 63 bits of precision, so result should be correctly rounded for 99.9% of input values, with less than 1 ULP error otherwise.

提交回复
热议问题