• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is NIO really faster than java.io.*

 
Sameer Amte
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have always used the java.io. classes for java IO. I tried to compare the performance of the io and nio classes FileChannel and ByteBuffers with the normal FileInput/OutputStreams and found that the normal java io was faster than nio.
I wrote a simple program that reads a file into a ByteBuffer/Byte Array and writes the output to a FileOutputStream/ByteBuffer.
The ByteBuffer requires additional operations that might be resulting in a delay. After every read you need to do a flip on a ByteBuffer and before every read(), you need to clear the buffer. These operations might be causing delays.
The code for the two approaches:





The Old IO class took less than 1 milisecond to complete and the new io class took 20 miliseconds to complete.

Any thoughts???

Thanks
Sameer
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java.io.* classes are implemented using NIO (for example, FileInputStream has a getChannel() method) so the speed difference between the two should be negligable. The improvement NIO introduced was not speed, but new and interesting functionality that java.io.* did not have. See this article for some examples.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!