I have read one tutorial on New IO package of Java. It says that Java NIO is faster than the old IO API. I have tried to find the difference between them using below programs
file.txt has about 30000 lines. When I run Main.java. I am getting below results
Time taken by NIOReader is 7 seconds
Time taken by IOReader is almost 0 seconds
I thought may be its caching the file somewhere so I tried running both of them individually as well. But I am getting the same result. I beleive I am doing something wrong here. Can ant one suggest me what is wrong or if I have misunderstood the whole concept.
pawan chopra wrote:I have read one tutorial on New IO package of Java. It says that Java NIO is faster than the old IO API.
That's a fast oversimplification. NIO introduced a bunch of functionality that the original java.io classes couldn't handle (see this article). The java.io classes were rewritten with NIO under the covers, so the performance should be similar under most circumstances.
You should also be very cautious about drawing conclusions from microbenchmarks. How the JVM behaves on a small snippet of code is probably not an accurate representation of how it will work in a non-trivial program.
Stephan van Hulst wrote:
The problem is that your code calls the channel.size() method in the for loop. In my case, more than 6 million times.
Thanks Stephan. You are right channel.size() was the problem. But I am surprise how come fetching size of some object is taking more than 10 seconds? Is it like every time it reads the number bytes from the file?
Thanks for the article its great tutorial.
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com