Isn't it amazing that, as time goes by, best practices that used to be foremost in your mind get pushed to the back? It isn't that you forgot them, but you can forget their importance until reminded.

At work, we have an application written in Java that process chunks of data. At the appropriate time, it writes out a summary of the information. So that we can stop and start the process and not have to restart processing at the beginning, it writes out the current summary of info. The summary looks something like:

ConcurrentHashMap<KeyBean, ConcurrentHashMap<SubKeyBean, DataBean>> data;

The KeyBean, SubKeyBean, and DataBean objects are simple java beans. As a quick and dirty hack, I was using an ObjectOutputStream to save the data.

This was clunky for lots of reasons; the main one is that if I changed the bean definitions, the new code couldn't read in the old data. So I changed it to output a tab separated text file. Since these beans are simple, this was an easy, low tech solution. I also liked that I could easily read the data file to see what the summary was at a point in time.

The surprise was that after making this change, the code ran much faster. I wondered how that was possible.

This brings us to the best practice that I knew, but didn't follow: always use a BufferedWriter or BufferedOutputStream. When written my text output, I had used a BufferedWriter, but when I used the ObjectOutputStream, I didn't.

I wrote a simple test program that generated some fake data and compared the results.

Serialization Method Time (in milliseconds)
Unbuffered ObjectOutputStream682
Buffered ObjectOutputStream34
Buffered Text76

Yowsa! It's an order of magnitude difference.

me = new Dummy();