Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

NIO vs IO

 
Ranch Hand
Posts: 419
Mac jQuery Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

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.


 
Saloon Keeper
Posts: 12251
259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit]

Actually, most of the time is spent on the for loop. On my system, it takes 14 seconds on a 6MB file, even when I comment out the buffer.get() statement.

The problem is that your code calls the channel.size() method in the for loop. In my case, more than 6 million times.

Call channel.size() once, store it in a variable, and use that variable in the loop.
 
Bartender
Posts: 9615
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
pawan chopra
Ranch Hand
Posts: 419
Mac jQuery Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:[edit]
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?

Joe,

Thanks for the article its great tutorial.
 
Surfs up space ponies, I'm making gravy without this lumpy, tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic