Java -- Stream, NIO ByteBuffer, NIO MappedByteBuffer performance comparison, mappedbytebuffer

Source: Internet
Author: User

Java -- Stream, NIO ByteBuffer, NIO MappedByteBuffer performance comparison, mappedbytebuffer

Currently, the most IO method in Java is a variety of file reading methods. This article compares the performance of Stream, NIO ByteBuffer, and nio MappedByteBuffer, so that we can know how to write code for reading highly performing files.

 

Package com. seeyon. nio; import org. junit. test; import java. io. *; import java. nio. byteBuffer; import java. nio. mappedByteBuffer; import java. nio. channels. fileChannel;/*** Created by yangyu on 16/12/28. * // ** compare the performance of Stream, NIO ByteBuffer, and nio MappedByteBuffer * the slowest Stream and NIO MappedByteBuffer * the fastest * Stream: 1000 ms * NIO ByteBuffer: 220 ms * NIO MappedByteBuffer: 112 ms */public class Compare {/*** use stream as the IO stream to read and write files * speed: around ms ** @ throws IOException */@ Test public void useStream () throws IOException {long startTime = System. currentTimeMillis ();/*** file of 4000000 integer length */int num = 2000*2000;/*** output stream with buffer, write File */DataOutputStream dataOutputStream = new DataOutputStream (new BufferedOutputStream (new FileOutputStream ("/Users/yangyu/Downloads/compare. tmp "); for (int I = 0; I <num; I ++) {dataOutputStream. writeInt (I);} dataOutputStream. close (); int data = 0;/*** input stream with buffer, read the file */DataInputStream in = new DataInputStream (new BufferedInputStream (new FileInputStream ("/Users/yangyu/Downloads/compare. tmp "); try {while (true) {data = in. readInt () ;}} catch (EOFException e) {System. out. println ("read completed" + data);} in. close (); long endTime = System. currentTimeMillis (); System. out. println ("ms:" + (endTime-startTime);}/*** use NIO ByteBuffer * Time: 220 ms * @ throws IOException */@ Test public void useNioByteBuffer () throws IOException {long startTime = System. currentTimeMillis (); int num = 2000*2000;/*** file output stream */FileOutputStream fileOutputStream = new FileOutputStream ("/Users/yangyu/Downloads/compare. tmp ");/*** NIO Channel */FileChannel fileChannel = fileOutputStream. getChannel ();/*** ByteBuffer buffer */ByteBuffer buffer = ByteBuffer. allocate (num * 5); for (int I = 0; I <num; I ++) {buffer. putInt (I);}/*** prepare for writing */buffer. flip ();/*** write operation */fileChannel. write (buffer); fileChannel. close ();/*** buffer */ByteBuffer buffer1 = ByteBuffer. allocate (num * 5);/*** file input stream */FileInputStream in = new FileInputStream ("/Users/yangyu/Downloads/compare. tmp ");/*** input channel */FileChannel fin = in. getChannel ();/*** prepare for reading */buffer1.clear (); System. out. println (buffer1.limit ();/***** read */fin. read (buffer1); fin. close (); long endTime = System. currentTimeMillis (); System. out. println ("ms:" + (endTime-startTime); buffer1.flip (); System. out. println (buffer1.limit ();}/*** use MappedByteBuffer to map the file to the memory through FileChannel * Time: 112 ms * @ throws IOException */@ Test public void useRandomAccess () throws IOException {long startTime = System. currentTimeMillis (); int num = 2000*2000;/*** use the random access location of RandomAccessFile */RandomAccessFile file = new RandomAccessFile ("/Users/yangyu/Downloads/compare. tmp "," rw ");/*** obtain Channel */FileChannel fileChannel = file. getChannel ();/*** map the file to the buffer MappedByteBuffer */MappedByteBuffer mappedByteBuffer = fileChannel. map (FileChannel. mapMode. READ_WRITE, 0, num * 4);/*** Write File */for (int I = 0; I <num; I ++) {mappedByteBuffer. putInt (I);} fileChannel. close (); int data = 0; RandomAccessFile file1 = new RandomAccessFile ("/Users/yangyu/Downloads/compare. tmp "," rw "); FileChannel fc = file1.getChannel (); MappedByteBuffer mappedByteBuffer1 = fc. map (FileChannel. mapMode. READ_WRITE, 0, file1.length ();/*** Read File */while (mappedByteBuffer1.hasRemaining () {data = mappedByteBuffer1.getInt ();} fc. close (); long endTime = System. currentTimeMillis (); System. out. println ("ms:" + (endTime-startTime); System. out. println (data );}}

 

The conclusion is very obvious. When I/O is used to read and write files later, I/O MappedByteBuffer will be used more. After all, NIO has much better performance than the old IO.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.