| Subcribe via RSS or via Email

Groovy: my new favourite programming language

September 17th, 2007 Posted in Groovy

Recently I discovered the Groovy programming language. Basically it is a dynamic language that runs on the JVM and compiles to .class files just as Java does. A better description can be found on the groovy homepage.

However much as I like the syntax and features of Groovy I am concerned that groovy going to be too slow for anything complex. Some reports put it orders of magnitude slower than plain Java. I struggled to find benchmarks, so I decided to write my own trivial test. The algorithm that all programmers learn early in their career, the Fibonacci sequence.

I decided to use the traditional (but less efficient) recursive method. I also stored each result into a list as it is processed, although this wasn’t used in the computation. The two files look as follows:

SpeedTestJava.java:

import java.util.ArrayList;
import java.util.List;

public class SpeedTestJava
{
	public static void main(String[] args)
	{
		List list = new ArrayList();
		for(int index = 0; index <= 35; index++)
		{
			long currentTime = System.currentTimeMillis();
			fib(index);
			currentTime = System.currentTimeMillis() - currentTime;
			list.add(currentTime);
			System.out.println(index + ")t" + currentTime);
		}
	}
	public static int fib(int num)
	{
		if(num == 0 || num == 1)
		{
			return num;
		}
		else
		{
			return fib(num - 1) + fib(num - 2);
		}
	}
}

SpeedTestGroovy.groovy:

class SpeedTestGroovy
{
	static void main(args)
	{
		def list = []
		for(index in 0..35)
		{
			def currentTime = System.currentTimeMillis()
			fib(index)
			currentTime = System.currentTimeMillis() - currentTime
			list << currentTime
			println index + ")t" + currentTime
		}
	}
	static int fib(int num)
	{
		if(num == 0 || num == 1)
		{
			return num;
		}
		else
		{
			return fib(num - 1) + fib(num - 2);
		}
	}
}

The results were quite surprising. For Fibonacci of 35 the Java execution time was 171ms (~ 2 hundredths of a second), while for Groovy the execution time was a staggering 103989ms (just over 100 seconds). The Java code was 2 orders of magnitude faster (don’t hear that much eh?), which is a stupidly large amount for such a simple task. It is clear that performance in Groovy has a long way to go. I personally hope that it does get there and does not get labeled with the ‘is too slow’ tag that has stuck with Java since it was introduced.

After demonstrating that Groovy is slow, it should be noted that because Groovy can call Java code natively then it is trivial to put any time consuming code into Java and then call this from Groovy, for example (provided that SpeedTestJava.class is on the classpath when compiling and running the Groovy Code) it is possible to do this:

class SpeedTestGroovyJava
{
	static void main(args)
	{
		def list = []
		for(index in 0..35)
		{
			def currentTime = System.currentTimeMillis()
			SpeedTestJava.fib(index)
			currentTime = System.currentTimeMillis() - currentTime
			list << currentTime
			println index + ")t" + currentTime
		}
	}
}

This code executes in approximately the same time as the pure Java solution and is the perfect way to use the strengths of both languages. It would, for example, be much simpler in groovy to do some processing on the results list, to build a GUI to allow the user to specify which Fibonacci number to compute or to write the results to an XML file (or any file in fact).

Well thats the end of my first real blog post, it was quite interesting for me writing it, learning about some the different wordpress tools, and listening to the legend that is Sir Paul McCartney on Radio One.

One Response to “Groovy: my new favourite programming language”

  1. Poppy Says:

    Welcome to blogging Richard! To me, Fibonacci is a character in Prison Break, but I still enjoyed reading all your clever coding :-)


Leave a Reply

retaggr