Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

New IO

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've started looked at the nio packages and wondering if I should consider some re-write for performance gain (No I have not run any test to determine if there is an I/O bottleneck, I am merely curious).

The following code:

(t.pl is a small 11 line perl script)
yields this output:
doFileChannel : 14ms
doFileStream : 1ms

Am I missing something here, the nio packages seem to be much more overhead and much slower. I'm sure there are particular cases where nio shine but basic file operations don't appear to be that.
 
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A couple points:

1) Be wary of testing order - I noticed that the slower test is the first one. This often happens as first invocations often have more overhead such as loading classes, jvm start up, and even loading the file into an OS cache.

2) Repeat tests - One repetition on a test, particularly one that is so fast won't be definitive. Repeat the test in a loop randomly determining the order of the invocations of the different approaches.

3) Be cautious when interpretting small execution times - Such small times are often misleading as the clock accuracy on certain machines is only say 10 ms.. So often times that are really the same will show up as say either 0 ms., or 10 ms. even though there is no real difference between the two. It was just random whether when the code ran if the OS clock was ready to tick or not.

4) Make sure you are testing the same things - As you extra code in one approach make sure that it is a fair comparison, i.e. it isn't doing more and different things on the data set.

5) Read a larger file - You may not get the full benefit of faster IO on a file that only takes a couple ms. to read. This is more a measurement of startup costs than IO costs.

I would address these problems in the test before interpretting any results.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code tests one particular way of using NIO; it's not very meaningful as a generalization about perfomance of NIO in all cases. And note this sentence from the API for map():

"For most operating systems, mapping a file into memory is more expensive than reading or writing a few tens of kilobytes of data via the usual read and write methods. From the standpoint of performance it is generally only worth mapping relatively large files into memory."

For small files, you're probably better off not using MappedByteBuffer. You may want to try using FileChannel's read() method to read in to a ByteBuffer, and then convert the ByteBuffer to s CharBuffer using a CharsetDecoder. Or not. Often NIO just ends up being more work to figure out, with little or no gain. There are specific niches where it really excels, but for what you seem to be doing (reading lines as Strings), I wouldn't expect any great improvements from NIO. If it weren't a text file, you'd have better luck with reading a raw ByteBuffer - but since it's text, the added complications probably aren't worth the effort.

If you're using JDK 5 or later, I'd just use a java.util.Scanner, which uses some NIO under the covers, and makes the whole process of reading text somewhat simpler than it was previously.
[ May 20, 2007: Message edited by: Jim Yingst ]
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys/gals, some good feedback.
 
Brian Spindler
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow! by changing to the following code block:



And changing to a 34M file, the output is now:

doFileStream : 1618ms
doFileChannel : 150ms

This output is the same regardless of which one is executed first (you can see I swapped them from previous example).
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. File FILE = new File("C:\\primes.txt");
2. FileChannel fileChannel = new FileInputStream(FILE).getChannel();

//MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
3. ByteBuffer buffer = ByteBuffer.allocate((int)fileChannel.size());
4.fileChannel.read(buffer);
5.CharBuffer cb = decoder.decode(buffer);
6.BufferedReader br = new BufferedReader(new CharArrayReader(cb.array()));
7.String line;
8.while ((line = br.readLine()) != null) {
9. System.out.println(" FileChannel : "+line);
10.}

in the above program , the line number 9 is not printing the line(String value which is read from the file) ,

if i add buffer.flip(); after 4th line , then i can able to print the line.

could you someone let me know is there any mistake above code .

Regards
Dhaneshkumar
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
DontWakeTheZombies. If you have a question, create a new topic.
reply
    Bookmark Topic Watch Topic
  • New Topic