About Jakub Kubrynski

I'm software developer by vocation. Java team leader at work and open source enthusiast at home. Strongly interested in new technologies. Fan of java.util.concurrent and sun.misc.Unsafe. My motto is "minimum code, maximum functionality.

Java 7 vs Groovy 2.1 Performance Comparison

I haven’t used Groovy for 2 years, since my last touch with Grails. I get stuck in (hard)core Enterprise Java, with some performance aspects in background. I’ve almost missed a chance to learn Spock, but fortunately Warsaw Java User Group helped me to snap out of some legacy systems and back to normal self-development. In fact I hope that frameworks such as Spock or Geb will change approach to writing tests, by making them easier and more effective. Both frameworks use Groovy, as well as the new king in build tools – Gradle. Seeing pace how Groovy impacts our daily routines I decided to look closer at its performance, and compare it to Java 7.

My test environment is based on Java 1.7.0_25 and Groovy 2.1.6. As always in such comparisons I used Caliper in version 1.0-beta-1 (almost stable) and prepared a number of (I hope) representative microbenchmarks.

First benchmark based on Fork/Join framework should be most similar in both languages, because it uses some native mechanisms. My test initialize array with some random int data, and then use framework to find biggest element in array. In Groovy my compute functions looks like below:

@Override
Integer compute() {
  def size = end - start
  if (size == 1) {
    Math.max(array[start], array[end])
  } else {
    int diff = size / 2
    MaxValueSeeker left = 
      new MaxValueSeeker(array, start, start + diff)
    left.fork()
    MaxValueSeeker right = 
      new MaxValueSeeker(array, start + diff, end)
    Math.max(right.compute(), left.join())
  }
}

Java version is of course very similar. After dozen minutes of measuring I get very promising result: Groovy is slower only… 8 times.

Now it’s time to check some more realistic in everyday development. I choosed simple POJO/POGO (yeah) with few simple operations just to be sure, that JIT won’t eliminate my code (and belive me he loves doing such jokes). My pseudo “business logic” method in groovy:

def int proceed(int reps) {
  List<GroovyPojo> list = new ArrayList<>()
  int sum = 0;
  reps.times {
    // first param is int and second is String
    list.add(new GroovyPojo(value: it, stringValue: it))
  }
  list.each {
    if (Integer.parseInt(it.stringValue) == it.value) {
      sum += it.value
    }
  }
  sum
}

Java version differs mostly by getters and manual String boxing in POJO constructor. One more time dozen minutes spent on reading news and… this time Groovy is slower only 7 times

The last test should be stressful and check both languages in more complex computations. I made up my mind and chose quicksort algorithm. Few loops, few if statements should work. I’m not going to copy-paste it here, because it’s well known solution.  What is worth to mention is of course timing result, outdistancing Groovy almost 5 times! But I’ve done some googling and noticed that Groovy 2.0 introduced @CompileStatic annotation, which should give us some additional performance boost. So lets check… Yes, with static compilation Java advantage fell to 220%.

In the table below you can find detailed results. To sum up – I’m not sure that using Groovy in mission critical functions is a good idea, but definitely it’s great solution for implementing tests, prototyping, etc. Just let me highlight, that writing Caliper’s results parser taken about 6 lines in Groovy (parse json, iterate over measurements and count average)

Performance comparison

MethodJava [ns]Groovy [ns]Factor
Fork/Join22.132181.0188.18
Pojos117.914856.3377.26
Quicksort68.728330.1594.80
Quicksort with @CompileStatic67.752147.7922.18

 

Reference: Java 7 vs Groovy 2.1 Performance Comparison from our JCG partner Jakub Kubrynski at the Java(B)Log blog.
Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

4 Responses to "Java 7 vs Groovy 2.1 Performance Comparison"

  1. SerCe says:

    Caliper? Use JMH!

  2. Gethin James says:

    Could you run the tests again using the groovy – indy.jar. That should use invoke Dynamic in Java 7.

  3. Hi James,

    I’ve tried to use indy support but unfortunately it was few times slower that without invoke dynamic. JIT was compiling methods all the time – I’ll try on JDK8 in few days to see if there is any improvement.

    Kind regards,
    Kuba

  4. Gethin James says:

    You may like to look at this quicksort : https://github.com/pledbrook/groovy-for-java-devs/tree/master/quicksort.
    It implements it in Java, Groovy, and Groovy with @CompileStatic. It profiles each of them to give you an idea of the performance differences.
    Regards,
    Gethin.

Leave a Reply


− one = 8



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books