Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #28529

Re: NIO not so hot

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news-1.dfn.de!news.dfn.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.java.programmer
Subject Re: NIO not so hot
Date Sun, 18 May 2014 13:38:30 +0200
Lines 46
Message-ID <btrkdnFjc3qU1@mid.individual.net> (permalink)
References <t24en9putph3mbnulkveghdjbrnisp105h@4ax.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Trace individual.net IzyhP9U47M54Fx4yW0E6kwLMmObQgOMTntnW5eJFaoldwCBrM=
Cancel-Lock sha1:vbbAu5E8M7C4GaNDggRmvYLVa3o=
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
In-Reply-To <t24en9putph3mbnulkveghdjbrnisp105h@4ax.com>
Xref csiph.com comp.lang.java.programmer:28529

Show key headers only | View raw


On 17.05.2014 13:28, Roedy Green wrote:
> I did a benchmark to read a file of bytes in one I/O then convert it
> to a String
>
>          // Using a random sample data file of 419,430,400 chars
> 419,430,400 bytes UTF-8.
>          // RandomAccess 1.46 seconds
>          // InputStream 1.48 seconds
>          // NIO 1.56 seconds
>
> NIO is great for grabbing bytes, but if you have to suck them out of
> the buffer, it does a get() call on every byte.

This is not true for all cases.  For example, if ByteBuffer and 
CharBuffer have an array this method is invoked, which will directly 
access these arrays:
sun.nio.cs.UTF_8.Decoder.decodeArrayLoop(ByteBuffer, CharBuffer)

> The code is posted at http://mindprod.com/jgloss/nio.html

The code suffers from too much copying: in readFileAtOnceWithNIO() you 
use a direct buffer, then need to copy it into a byte[] (which btw. does 
not use individual get() for every byte, see 
java.nio.DirectByteBuffer.get(byte[], int, int)) and then you create the 
String (which copies the data again but that cannot be avoided).  If you 
use a heap byte buffer one level of copying can be omitted, because you 
can access the byte[] inside and create the String with this constructor:

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte[], 
int, int)

However, the test is quite unrealistic since this is not how NIO is 
usually used.  The whole purpose of Buffer and subclasses is to read 
data in chunks.

I have extended the recent test case for char decoding to include NIO. 
Because NIO character decoding I created a version which does a rough 
CRC calculation so I was able to verify my implementation read all the 
characters in the proper order.  You can find all the code here:

https://gist.github.com/rklemme/fad399b5e7cc4d3b6d0c

Kind regards

	robert

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-17 04:28 -0700
  Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 13:38 +0200
    Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 13:38 +0200
      Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 14:12 +0200
        Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-18 19:09 +0200
          Re: NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-26 00:51 -0700
            Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-26 20:14 +0200
    Re: NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-26 01:15 -0700
    Re: NIO not so hot Roedy Green <see_website@mindprod.com.invalid> - 2014-05-26 01:24 -0700
      Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-05-26 23:50 +0200
  Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-05-31 02:29 -0700
    Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-06-01 12:15 +0200
      Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-06-02 07:37 -0700
        Re: NIO not so hot Robert Klemme <shortcutter@googlemail.com> - 2014-06-03 23:07 +0200
          Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-06-06 01:32 -0700
      Re: NIO not so hot Rupert Smith <rupertlssmith@googlemail.com> - 2014-06-02 07:46 -0700

csiph-web