Nio performance bottleneck

Discussion in 'Java' started by JLM, Nov 24, 2004.

  1. JLM

    JLM Guest

    Hello !

    In order to be sure that nio package was more performant than regular
    streams, i have tested it with this little piece of code :

    // ----------------------------
    ByteBuffer loWriteBuffer = ByteBuffer.allocateDirect(1024);
    CharBuffer loBuffer = CharBuffer.allocate(512);

    Charset charset = Charset.forName("ISO-8859-1");
    CharsetEncoder encoder = charset.newEncoder();
    long llMillis, llMillis2, llMillis3;

    llMillis = System.currentTimeMillis();
    try {
    FileChannel loChannel = new RandomAccessFile(new
    File("C:\\Temp\\Test.txt"), "rw").getChannel();
    for (int i = 0; i < 1000; ++i) {
    loBuffer.put("ceci est un test\r\n");
    loBuffer.flip();
    encoder.encode(loBuffer, loWriteBuffer, false);
    loWriteBuffer.flip();
    loChannel.write(loWriteBuffer);
    loBuffer.clear();
    loWriteBuffer.clear();
    }
    loChannel.close();
    }
    catch (IOException leIO) {
    leIO.printStackTrace();
    }
    llMillis2 = System.currentTimeMillis();
    try {
    Writer loWriter = new BufferedWriter(new OutputStreamWriter(new
    FileOutputStream("C:\\Temp\\Test2.txt"), charset));
    for (int i = 0; i < 1000; ++i) {
    loWriter.write("ceci est un test\r\n");
    }
    loWriter.flush();
    loWriter.close();
    }
    catch (IOException leIO) {
    leIO.printStackTrace();
    }
    llMillis3 = System.currentTimeMillis();

    System.out.println("Results : nio = " + (llMillis2 - llMillis) + " ms /
    standard = " + (llMillis3 - llMillis2) + " ms");
    // ----------------------------

    I was surprised to see that, on my computer, the regular stream based part
    was running about 4 times quicker than the nio based part !!
    Here is my question : is my code correctlty optimized ? Anybody already
    noticed this performance bottleneck ? Is my test the cause of this problem ?

    Thank you !
    Jean-Luc
    JLM, Nov 24, 2004
    #1
    1. Advertising

  2. JLM

    Yu SONG Guest

    JLM wrote:
    > Hello !
    >
    > In order to be sure that nio package was more performant than regular
    > streams, i have tested it with this little piece of code :
    >

    ....

    > for (int i = 0; i < 1000; ++i) {
    > loBuffer.put("ceci est un test\r\n");
    > loBuffer.flip();
    > encoder.encode(loBuffer, loWriteBuffer, false);
    > loWriteBuffer.flip();
    > loChannel.write(loWriteBuffer);
    > loBuffer.clear();
    > loWriteBuffer.clear();
    > }
    > loChannel.close();
    > }

    ....
    >
    > I was surprised to see that, on my computer, the regular stream based part
    > was running about 4 times quicker than the nio based part !!
    > Here is my question : is my code correctlty optimized ? Anybody already
    > noticed this performance bottleneck ? Is my test the cause of this problem ?
    >



    You don't need to encode and flip (twice) each time.

    For most of my applications, I usually use a big enough buffer to
    process all the data first, and then write this buffer to the file once
    or twice.


    --
    Song

    /* E-mail.c */
    #define User "Yu.Song"
    #define At '@'
    #define Warwick "warwick.ac.uk"
    int main() {
    printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
    return 0;}

    Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
    _______________________________________________________
    Yu SONG, Nov 25, 2004
    #2
    1. Advertising

  3. JLM

    JLM Guest

    "Yu SONG" <> a écrit dans le message de news:
    co4968$8e3$...
    > JLM wrote:
    >> Hello !
    >>
    >> In order to be sure that nio package was more performant than regular
    >> streams, i have tested it with this little piece of code :
    >>

    > ...
    >
    >> for (int i = 0; i < 1000; ++i) {
    >> loBuffer.put("ceci est un test\r\n");
    >> loBuffer.flip();
    >> encoder.encode(loBuffer, loWriteBuffer, false);
    >> loWriteBuffer.flip();
    >> loChannel.write(loWriteBuffer);
    >> loBuffer.clear();
    >> loWriteBuffer.clear();
    >> }
    >> loChannel.close();
    >> }

    > ...
    >>
    >> I was surprised to see that, on my computer, the regular stream based
    >> part was running about 4 times quicker than the nio based part !!
    >> Here is my question : is my code correctlty optimized ? Anybody already
    >> noticed this performance bottleneck ? Is my test the cause of this
    >> problem ?
    >>

    >
    >
    > You don't need to encode and flip (twice) each time.
    >
    > For most of my applications, I usually use a big enough buffer to process
    > all the data first, and then write this buffer to the file once or twice.


    It is only a test case, and it is intentionaly fractionned : i wanted to
    test 1000 independent invocations of a complete write. You can assume that
    each loop is not related with the others. But in fact you are right on a
    point : my test is not good, because the two parts in my program have not
    exactly the same behaviors. To really have two equivalent parts, i had to
    change the source code on the twice part : i have to flush the output stream
    in every loop, instead of flushing once at the end. In that case, the NIO
    part is faster than the standard case. Honnor is safe ! ;-)
    Thank you giving to me a clue to find the solution ! :)

    >
    > --
    > Song
    >
    > /* E-mail.c */
    > #define User "Yu.Song"
    > #define At '@'
    > #define Warwick "warwick.ac.uk"
    > int main() {
    > printf("Yu Song's E-mail: %s%c%s", User, At, Warwick);
    > return 0;}
    >
    > Further Info. : http://www.dcs.warwick.ac.uk/~esubbn/
    > _______________________________________________________
    >
    JLM, Nov 25, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glenn

    Performance Bottleneck in ASP.NET

    Glenn, Jan 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    565
    Glenn.
    Jan 8, 2004
  2. =?Utf-8?B?bWF0dmRs?=

    Access to remote application and bottleneck

    =?Utf-8?B?bWF0dmRs?=, Jul 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    346
    =?Utf-8?B?bWF0dmRs?=
    Jul 10, 2005
  3. David Zimmerman
    Replies:
    1
    Views:
    2,106
    Harald Kirsch
    Jul 22, 2003
  4. Roedy Green
    Replies:
    6
    Views:
    3,277
    Steve Horsley
    Jul 23, 2003
  5. iksrazal

    NIO with timeouts != NIO?

    iksrazal, Jun 17, 2004, in forum: Java
    Replies:
    1
    Views:
    6,229
    iksrazal
    Jun 18, 2004
Loading...

Share This Page