.

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 void main(String[] args) {

long then = System.nanoTime();

double x=0;

for (int i = 0; i < (int)1e8; i++)

x=x+Math.sin(i)/Math.cos(i);

double itime = ((System.nanoTime() - then)/1e9);

System.out.println("Time for calculations (sec): " + itime+"\n");

System.out.println("Pi = " + x +"\n");

}

}

and processed using DataMelt with OpenJDK13 further increases the execution speed by a factor 2 compared to the Groovy dynamic language.

Similar benchmarks of the Java code have been carried out by repeating the calculation using Java SE 8 ("JDK1.8") released in March 2014. The computation was about a factor 8 slower than for the OpenJDK13. This was due to less optimized code for evaluation of trigonometric functions in JDK1.8 (and earlier versions). This confirms significant improvements for numeric computations in the recent JVM compared to the previous releases.

The question of code profiling using different implementations is a complex problem, and we do not plan to explore all possible scenarios in this article. The main conclusion we want to draw in this section is that the processing speed of the code that implements mathematical functions for numeric calculations is substantially better for Groovy than for CPython2 (CPython3). The observed performance improvements in dynamically-typed languages implemented in Java are due to the recent enhancements in the modern JVMs, leading to a large factor in the speed of evaluations

of mathematical functions.

Sergei Chekanov

- Toucan Toco unveils native integration for Snowflake
- Top trends in big data for 2021 and the future
- Common application layer protocols in IoT explained
- HYCU Protégé integrates Kubernetes data protection
- 10 Jenkins alternatives for developers
- Flow efficiency is one of the trickiest DevOps metrics
- Continuous delivery vs. continuous deployment: Which to choose?
- Camel case vs. snake case: What's the difference?
- Advice on intent-based networking and Python automation
- Risk & Repeat: Will the Ransomware Task Force make an impact?

Posted 3 May 2021

© 2021 TechTarget, Inc. Powered by

Badges | Report an Issue | Privacy Policy | Terms of Service

**Most Popular Content on DSC**

To not miss this type of content in the future, subscribe to our newsletter.

- Book: Applied Stochastic Processes
- Long-range Correlations in Time Series: Modeling, Testing, Case Study
- How to Automatically Determine the Number of Clusters in your Data
- New Machine Learning Cheat Sheet | Old one
- Confidence Intervals Without Pain - With Resampling
- Advanced Machine Learning with Basic Excel
- New Perspectives on Statistical Distributions and Deep Learning
- Fascinating New Results in the Theory of Randomness
- Fast Combinatorial Feature Selection

**Other popular resources**

- Comprehensive Repository of Data Science and ML Resources
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- 100 Data Science Interview Questions and Answers
- Cheat Sheets | Curated Articles | Search | Jobs | Courses
- Post a Blog | Forum Questions | Books | Salaries | News

**Archives:** 2008-2014 |
2015-2016 |
2017-2019 |
Book 1 |
Book 2 |
More

**Most popular articles**

- Free Book and Resources for DSC Members
- New Perspectives on Statistical Distributions and Deep Learning
- Time series, Growth Modeling and Data Science Wizardy
- Statistical Concepts Explained in Simple English
- Machine Learning Concepts Explained in One Picture
- Comprehensive Repository of Data Science and ML Resources
- Advanced Machine Learning with Basic Excel
- Difference between ML, Data Science, AI, Deep Learning, and Statistics
- Selected Business Analytics, Data Science and ML articles
- How to Automatically Determine the Number of Clusters in your Data
- Fascinating New Results in the Theory of Randomness
- Hire a Data Scientist | Search DSC | Find a Job
- Post a Blog | Forum Questions