Groovy Micro Benchmark

Posted by jonchase

Groovy Micro BenchmarkFollowing on from the previous post (and here’s the last post), here’s a very quick and dirty, completely unscientific, prone to error, your mileage may vary micro benchmark I did using the code in the last post.

Date set: a String containing roughly 450 XML elements in the form <contact name=”some name” email=”some email” />

Methodology: I wrote driver programs in both Groovy and Java to run the test. Here’s the one from Java (the Groovy one is similar):

Driver program

As you can see from the code, very quick and dirty. The test iterates 1,000 times and then prints out the time consumed.

I ran the test several times on my laptop (2Ghz, 2GB ram, blah blah blah it doesn’t really matter here) and here are the general results for total execution time for 1,000 iterations:

  • Java: ~9 seconds
  • Groovy: ~28 seconds

Quite a difference. I suspect part of that has to do with my inefficiency as a Groovy programmer. Certainly though, you can see that there is measurable difference in performance. I wonder what type of XML parser the Groovy implementation uses behind the scenes. The Java implementation uses SAX. If Groovy were using DOM with XmlSlurper, I could see that making a very large difference. Any experts care to chime in?

Now, before you decide to write off Groovy, another metric of interest might be seconds required to develop the code. It was something like this:

  • Java: ~ 1,800 seconds - roughly 30 minutes. (First I had to decide what XML library I was going to use - JDOM? Dom4J? Xstream? Then I had to search and search for documentation on JDOM…and of course I had to download JDOM, well, that is, I have to add JDOM as a dependency in my Maven pom.xml (after I looked up the current version of JDOM at the ibiblio repo), and rebuild my Eclipse classpath with the new JAR on it, and then figure out how to use the JDOM API…) I’ve been writing Java for about 6 years, so I consider myself fairly adept.
  • Groovy: ~300 seconds - roughly 5 minutes. (I had to find the page on the Groovy wiki that explained how to process XML.) I’ve been writing Groovy for less than 2 weeks.

So there you have it, my quick and dirty, completely unscientific, prone to error, your mileage may vary micro benchmark.

I’m not one to reserve judgment, so here goes:

  • First, this is just one example. Comprehensively measuring performance is a complex thing, and this benchmark doesn’t claim to do that. There is no such thing as saying “Groovy is slow”.
  • Second, I’d gladly trade the frustration and time requirement of the Java solution for the easier to read and write Groovy solution. Servers are cheap. I’ll almost always choose to pay the price in $$$ (which I can always get more of) rather than time (which I can never have enough of).

Update: here’s the final post in this series.

Subscribe to Jon's Blog Subscribe


5 Responses to “Groovy Micro Benchmark”

  1. fletch Says:

    Captain Obvious, is that you?

  2. Francois MAROT Says:

    I could not agree more with you ! Especially your last sentence ;)

  3. Groovy Micro Benchmark Revisited (Groovy is fast…) | Jon Chase's Blog Says:

    […] the last post, we saw the following results for parsing an XML dataset with about 450 simple elements in it 1,000 […]

  4. jonchase Says:

    Fletch,

    Have a look at the new benchmark where Groovy and Java performance are basically equal: http://www.juliesoft.com/blog/jon/index.php/2008/03/12/groovy-micro-benchmark-revisited-groovy-is-fast/

  5. Groovy is Coming… | Jon Chase's Blog Says:

    […] here are the second and third posts in this series (benchmarks […]

Leave a Reply