Sunday, August 7, 2011

This weekend I tried to compare Java file I/O with Groovy on two fronts:

a. Code bevity.
b. Performance (time taken to execute task.)

Groovy code was 34 lines and Java came out to be 48. The groovy code could be shorter but somehow I couldn't get the multiple assignments working. Got run time error.

On the performance front Java was a winner. Java code took 425 ms and Groovy code took 37112 ms. I was expecting Groovy to be slower, but not by this margin!

So I sent an email to the Groovy user group and got a very prompt reply back from one of the users. He mentioned that he wasn't sure that the difference (in performance) I've measured will probably hold for other environments. I am thinking that might be true. But, I thought I would put this post out there and get other opinions about Groovy vs Java performance difference they have seen / measured and tips/tricks to get around it?

Some code is omitted for brevity. You can download the source code from GitHub repository.
for (final int i in 1..200000) {
    accountNumber = rand.nextInt(1001011)
    customerNumber = rand.nextInt(2002110)
    int totalMinsUsed = rand.nextInt(1000)
    int totalMinsAvail = totalMin - totalMinsUsed

    String line = "${accountNumber}, ${customerNumber}, ${totalMin}, ${totalMinsUsed++}, ${totalMinsAvail}, ${price}, John Smith\n"
    if (i == 1) {
        outputFile.write(line)
    } else {
        outputFile.append(line)
    }
}

No comments:

Post a Comment

Your comments are welcome. It will help me improve my communication and content...plus it will encourage me! :-)