Java

Java Performance: IO vs NIO

By Max
February 17, 2014
0

Java NIO has been around for a while but i never had the chance to test it.
I have continued work on a data analyser project and for which i have optimized the parsing as presented in this post.
Anyway all that conversion was taking allot of time, so i have decided to convert the CSVs to binary files, and see how much performance can i gain along with shrinking the file sizes.

So here’s the opportunity for testing NIO 😀
I have written a small converter reading CSVs and writing binary files using NIO, but here the performance was not that important, since the writing would now be very rare. The resulting file had around 13M records.
Then i tried two different methods to read that file. First one was using standard IO:


	public void readIo() throws FileNotFoundException {
		File file = new File(FILE_NAME);
		BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
		DataInputStream fin = new DataInputStream(in);
		try {
			while (true) {
				long stamp = fin.readLong();
				double buy = fin.readDouble();
				double sell = fin.readDouble();
			}
		} catch (IOException e) {
			....
		}
	}

Reading using io took an average of 8066ms on my old dev laptop.
Speed was ‘decent’ compared to the csv files, as long as you remember to wrap the InputStream in a BufferedInputStrem, otherwise it will never finish.

Let’s see the NIO code:


	public void readNio() throws FileNotFoundException {
		File file = new File(FILE_NAME);
		FileChannel channel = new FileInputStream(file).getChannel();
		ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE);
		try {
			channel.read(buffer);
			buffer.flip();
			while (true) {
				if (buffer.remaining() < 24) {
					buffer.compact();
					int x = channel.read(buffer);
					if (x < 0) {
						channel.close();
						return;
					}
					buffer.flip();
				}
				long stamp = buffer.getLong();
				double buy = buffer.getDouble();
				double sell = buffer.getDouble();
			}
		} catch (IOException e) {
		     ...
		}
	}

A bit more to write, but the average was 3162ms so more than 250% improvement 😉

Comments: 0

Leave a Reply

Your email address will not be published. Required fields are marked *