Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

faster Math.exp() via JNI?

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()??

I tried just jni + C, but it's slower than just plain java.

like image 672
Dan Avatar asked Sep 15 '08 20:09

Dan


1 Answers

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.

like image 123
Renaud Avatar answered Sep 30 '22 06:09

Renaud