Tuesday, March 11, 2014

Groovy: Know Thine File I/O

Groovy is the topic of the day! And specifically groovy file IO.

As a disclaimer, I'm lazy with my file IO. As lazy as I can be. Which is why I love left shift
(<<) in groovy. You don't have to worry about streams, opening or closing files, nothing. Groovy goodness takes care of it all for you. But with most syntactic shortcuts, there can be a catch.

Let's look at an example.


This code took over 6 minutes to write a 600 KB 3172 line file. This is easy to write (go left shift!) but it's also a really bad way to do it. Let's look at a better method:


It's an extra line, and you don't get to use left shift, but this snippet was able to write the same file in 0.07 seconds. Why do you ask? The answer is really in what is missing from the Groovy File API documentation examples: There is no instruction to close the file. As it turns out, left shift is a complete operation that opens the file, writes your object to the end, and closes the file again. And it's that opening and closing of the file that takes a lot of time. The second code block keeps the file open for writing until it is actually done.

Technically this is a linear function, but the added time of waiting for the operating system to open the file is significant enough to actually matter here.

Lesson learned: Groovy is cool, and the things it will take care of for you are convenient, but you should always know what those short cuts are doing in the background. If you don't, you could wind up writing extremely inefficient (or even broken) code.