• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileReader VS. FileChannel  RSS feed

 
Brad Baker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am not sure if this is best hear or in performance but here goes:
I have a function that I am updating to use NIO
void build(File f) throws IOException {
Reader r = new BufferedReader(new FileReader(f));
StreamTokenizer st = new StreamTokenizer(r);
... do stuff ...
}
void build(File f) throws IOException {
FileChannel fc = (new FileInputStream(f)).getChannel();
Reader r = new BufferedReader(Channels.newReader(fc, "ISO-8859-1"));
StreamTokenizer st = new StreamTokenizer(r);
... do stuff ...
}
After the change the method is about 12% SLOWER then before. Now I am very new to NIO and I know that I must be overlooking something.

Any suggestions?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is it you're trying to achieve? Is there some particular property of channels that you're hoping to take advantage of here? Offhand I don't see any reason to expect to improve performance here. I note that in your original code you use platform default encoding, while the new code expicitly uses ISO-8859-1. Any reason? It seems reasonable to think that using non-default encoding may take a little bit longer to run. For comparison, try putting

in your original code, to see how that affects performance. You may also benefit from running the following:
 
Brad Baker
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,
Thank you for the reply. I guess I bought into the hype that NIO was a silver bullet for all Java IO. Just by changing to it I believed that there would be a HUGE speed increase.
Like all ASSumptions if one is not careful you just become the first three letters of it....
Best Regards,
Brad
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should say that I haven't really used the nio stuff much yet myself, so maybe there is some way of making this much faster. I see that for very large files it's suggested that using FileChannel's map() method may be helpful - but the overhead is too high if you're dealing with smaller files. Maybe there are other tricks that would apply to your problem. I guess I'm overdue to look into the nio classes in more detail...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!