Most float operations take about 1 ns in Java, so I'm not sure how much faster you expect them to be in C ++.
However, JNI calls often take about 30 ns, so if you do not perform many floating point operations for each call, you are worth more than you save.
As follows from the following micro-test, after the code warms up, each operation is subnano second.
If you want this to accelerate, you can use several cores and make them 4 times or faster.
public static void main(String[] args) throws Exception { int length = 200000; double[] a = fill(new double[length]); double[] b = fill(new double[length]); double[] c = fill(new double[length]); double[] x = new double[length]; for (int i = 0; i < 10; i++) testTime(length, a, b, c, x); } private static void testTime(int length, double[] a, double[] b, double[] c, double[] x) { long start = System.nanoTime(); for (int i = 0; i < length; i++) x[i] = a[i] * b[i] + c[i]; long time = System.nanoTime() - start; System.out.printf("Average time per double operation was %.1f ns%n", time / 2.0 / length); } private static double[] fill(double[] doubles) { for (int i = 0; i < doubles.length; i++) doubles[i] = Math.random(); return doubles; }
prints
Average time per double operation was 10.9 ns Average time per double operation was 17.9 ns Average time per double operation was 1.7 ns Average time per double operation was 1.0 ns Average time per double operation was 0.9 ns Average time per double operation was 0.8 ns Average time per double operation was 0.9 ns Average time per double operation was 0.8 ns Average time per double operation was 1.0 ns Average time per double operation was 0.9 ns
Peter Lawrey
source share