简体   繁体   中英

Why the NIO code is faster than that of Java IO?

I have lots of quests about Java NIO. I have read many articles where the discussion was digged deeper about it. But I really don't know in which aspects the NIO is quicker than IO.

Also I have observed that downloading a 100MB file with Java NIO code is at least 10 times faster than downloading with Java IO code.

Now my question regarding the fact is:

Suppose I am downloading a file is 1KB. In this case, will NIO code still be ten times faster for a 1KB file?

Generally speaking, NIO is faster than classic Java IO because it reduces the amount of in-memory copying. However, a ten-fold improvement in speed is implausible, even for large files. And when we are talking about downloading files (rather than reading / writing them to disk), the performance is likely to be dominated by the bandwidth and end-to-end latency to the machine you are loading from.

Finally, you are likely to find that the relative speedup of NIO for small files will be even less ... because of the overheads of establishing network connections, sending requests, processing headers and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM