Sunday, August 7, 2011

Groovy vs Java file I/O comparison, and how to improve it using withWriter.

**I learnt by making a mistake and hope to help someone with this post.

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! I was hoping I was doing something wrong.

Some code is omitted for brevity. You can download the source code from GitHub repository.
final File outputFile = new File(/D:\code\IProjects\GroovyPlayground\BigFileInGroovy.txt/)
... //Code omitted for brevity
for (final int i in 1..200000) {
    ... //Code omitted for brevity
    String line = "${accountNumber}, ${customerNumber}, ${totalMin}, ${totalMinsUsed++}, ${totalMinsAvail}, ${price}, John Smith\n"
    if (i == 1) {
        outputFile.write(line)
    } else {
        outputFile.append(line)
    }
}

So I sent an email to the Groovy user group and got prompt replies back from the users. Thanks to the awesome Groovy community I learnt what I was doing wrong. I was using the append method on the File object directly, which was creating a new reader, opening the file, writing the line at the end of the file and then closing the file and reader. Quoting Jochen Theodorou: "In your Java program you use BufferedWritter#append and in Groovy you use File#append. The big difference between those two methods is, that File#append, has to create a new reader, open the file, go to its end and there attach the line, just to close file and reader again. This takes a lot of time and burns away your performance."

So the trick to improve performance was to use withWriter on the File object that provides a buffered writer for the file in the context of the closure:

outputFile.withWriter('UTF-8') { writer ->
        String line = "${accountNumber}, ${customerNumber}, ${totalMin}, ${totalMinsUsed++}, ${totalMinsAvail}, ${price}, John Smith\n"
        writer << (line)
}
Now the execution time is 671 msec. Way better then previous 37112 msec.

Big thanks to Leonard Axelsson and Jochen Theodorou and in general to everyone who replied to my question on Groovy user group!

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)
    }
}

Monday, August 1, 2011

NFJS Experience - What I gained from it (Part I)

I had the opportunity to be at NFJS from Jul 22-24 sponsored by Griffin Solutions Group. It was my first experience and an experience that is difficult to describe in words. There were so many great sessions, so much to learn from a myriad of speakers covering different technologies.

I aim to cover the technologies that I learnt about in more detail in future blogs. Here I will cover a few main points that spoke to me / I learnt / I noticed :

a. I noticed a lot of momentum being gained in JVM languages. There were a lot of sessions covering Groovy, Scala, Clojure and related technologies around these languages. For example: Spock, Geb (pronounced Jeb) for better unit and functional testing using Groovy.

It's exciting. I just hope that more companies adopt these technologies or at least use them in addition to Java being their primary language (doesn't have to be that way though ;-)).

b. A couple of sessions had great ideas about how to be a better programmer. Specifically the session "Hacking Your Brain for Fun and Profit" from Nathaniel Schutta blew my mind. It had a lot of useful information on how to be more productive. If you happen to be at a conference where Nathaniel is presenting the talk, please do not miss it!

c. Related to JVM based languages again: In general I feel we are evolving towards higher level languages. Java was a breath of fresh air as compared to C++. No more managing memory allocation. That in itself was worthy of gold for me :-). Now, it's time to move away from handcuffs that Java puts around us and start concentrating more on business level logic. Groovy, Scala provide a lot of freedom from writing boiler plate stuff in addition to providing powerful features like "Closures", Meta-Programming (more on them in a later post).

Personally, I gained a lot from conversing with fellow attendees and the speakers. I would highly recommend NFJS to any programmer who cares about his trade. It will reinvigorate you to learn new technologies that in turn will help you solve problems more efficiently!

In a later post, I would share a couple of things I am trying that I learnt from the conference, that are making a difference in my life!

Were you at NFJS or any other tech conference? What did you learn? What spoke to you personally? Have a different opinion than mine? Please share. I would love to hear from you!