So... I always wondered, whether Java's Math.sqrt() would be faster than a call to the JNI, calling <math.h>'s/C's sqrt().

If anyone else wondered, here is the result, printed by my program:

Starting to initialize values.

Initializing finished. Starting Java's Math.sqrt():

Time taken with Math.sqrt(): 8767

Starting native C++'s sqrt():

Time taken with native sqrt(): 77396

The Java code is the following (REALLY trivial):

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| package org.matheusdev.jni;
import java.util.Random;
public class SQRTNative { static { System.loadLibrary("SQRTNative"); } private native static double sqrt(double d); public static void main(String[] args) { final long repeat = 2330000000L; final int values = 1000000; long time; System.out.println("Starting to initialize values."); double[] vals = new double[values]; Random rand = new Random(); for (int i = 0; i < values; i++) { vals[i] = rand.nextDouble()*1000; } System.out.println("Initializing finished. Starting Java's Math.sqrt():"); time = System.currentTimeMillis(); for (long i = 0; i < repeat; i++) { Math.sqrt(vals[(int)(i%values)]); } System.out.println("Time taken with Math.sqrt(): " + (System.currentTimeMillis()-time)); System.out.println("Starting native C++'s sqrt():"); time = System.currentTimeMillis(); for (long i = 0; i < repeat; i++) { sqrt(vals[(int)(i%values)]); } System.out.println("Time taken with native sqrt(): " + (System.currentTimeMillis()-time)); }
} |

And the code inside the "libSQRTNative.so" (running linux 64 bit here

) is simple too:

1 2 3 4 5 6 7 8 9
| #include "jni.h" #include <math.h> #include "SQRTNative.h"
JNIEXPORT jdouble JNICALL Java_org_matheusdev_jni_SQRTNative_sqrt(JNIEnv *env, jclass cls, jdouble d) { return sqrt(d); } |

Built with "QtCreator's build lib for release", aka GCC. Haven't messed around with any build flags

One thing more to add: I use "only" 1 MIO values, cause I can't even create an Array of the size of 2.330.000.000 doubles, and besides, it would crash with an OutOfMemoryException anyways.

I used exactly 2.330.000.000, cause I have a running 2.33 GHz cpu at that point, so you can easily see, that Java's sqrt takes about 8-9 clock ticks per computation, and the C++'s one takes aabout 77 cpu clock ticks.

These values are not perfect, because of the system needing resources, the Chromium running with 13 Tabs, the Eclipse running, one Dolphin running and a QtCreator running.

I assume that most time spent in the native version is the JNI overhead.

Also, this was a JNI-learing-and-have-fun-with-performance-tests-test-project, keep that in mind, and correct me, if I could do something better