Saturday, July 25, 2009

Groovy and Java Performance Comparison

First of all, I don't want to get into discussions about the advantages or disadvantages of one or the another. Java is a compiled language while Groovy is an scripting language. Java lacks of certain features that makes the life easier for developers, on the other side Groovy makes programming simple (most of the times).

But in this post what I want to show is the performance of each of them, so people (and me :) ) can decide which to use in a specific case.

Java | Groovy
2766 ms | 20121 ms
2828 ms | 19188 ms
2891 ms | 19188 ms
2969 ms | 19062 ms
2938 ms | 19281 ms
2813 ms | 20067 ms

The conditions of these test were:

IDE: Eclipse

Machine: Intel Core 2 Duo 2.1 Ghz, 3Gb RAM, Windows 7 RC

The test program was about filling a list of integers with consecutive numbers from 1 (one) to 10000000 (ten millions) , and them sum them up and showing the results.

Here is the code in Java:

import java.util.*;

public class TestJava {

public static void main(String[] args) {
long iniTime = System.currentTimeMillis();

List list = new ArrayList();
for (int i = 1; i <= 10000000; i++) {
list.add(i);
}
long sum = 0;
for (int i = 0; i < list.size(); i++) {
sum += list.get(i);
}

System.out.println("Java: The result is " + sum + " and took " + (System.currentTimeMillis() - iniTime) + " ms");
}

}

And the Groovy code (Pretty Simple :) ):

import java.lang.System

def iniTime = System.currentTimeMillis();

def list = []

for(i in 1..10000000){
list << i
}

long sum = 0
for(i in 0..<list.size){
sum += list[i]
}

println "Groovy: The result is ${sum} and took " + (System.currentTimeMillis() - iniTime) + " ms"

No comments:

Post a Comment