Recent Java enhancements for numeric calculations

In the past, slow evaluation of mathematical functions and large memory footprint were the most significant drawbacks of Java compared to C++/C for numeric computations and scientific data analysis. However, recent enhancements in the Java Virtual Machine (JVM) enabled faster and better numerical computing due to several enhancements in evaluating trigonometric functions.

In this article we will use the DataMelt (https://datamelt.org) for our benchmarks. Let us consider the following algorithm implemented in the Groovy dynamically-typed language shown below. It uses a large loop, repeatedly calling the sin() and cos() functions. Save these lines in a file with the extension “goovy” and run it in DataMelt:


import java.lang.Math
long then = System.nanoTime()
double x=0
for (int i = 0; i < 1e8; i++)
x=x+Math.sin(i)/Math.cos(i)
itime = ((System.nanoTime() - then)/1e9)
println "Time: " + itime+" (sec) result="+x

The execution of this Groovy code is typically a 10-20% faster than for the equivalent code implemented in Python:


import math,time
then = time.time()
x=0
for i in xrange(int(1e8)):
x=x+math.sin(i)/math.cos(i)
itime = time.time() - then
print("Time:",itime," (sec) result=",x)

Note that CPython2 (version 2.7.2) is a 20% faster than CPython3 (version 3.4.2), but both CPython interpreters are slower for this example than Groovy.

The same algorithm re-implemented in Java:


import java.lang.Math;
public class Example {
public static

 

 

 

To finish reading, please visit source site