Java--stream,nio Bytebuffer,nio Mappedbytebuffer Performance Comparison

Source: Internet
Author: User

At present, the most IO in Java has a variety of file reading methods, this article compares the performance of Stream,nio Bytebuffer,nio Mappedbytebuffer, let us know how to write high performance file read code.

 PackageCom.seeyon.nio;Importorg.junit.Test;ImportJava.io.*;ImportJava.nio.ByteBuffer;ImportJava.nio.MappedByteBuffer;ImportJava.nio.channels.FileChannel;/*** Created by Yangyu on 16/12/28.*//*** Compare Stream stream, NiO Bytebuffer,nio mappedbytebuffer performance comparison * with stream slowest, NiO mappedbytebuffer fastest * stream:1000ms * NiO bytebuf fer:220ms * NIO mappedbytebuffer:112ms*/ Public classCompare {/*** Read and write files using stream as IO stream * Speed: Around 1000ms * *@throwsIOException*/@Test Public voidUsestream ()throwsIOException {LongStartTime =System.currenttimemillis (); /*** 4 million integer-length files*/        intnum = 2000 * 2000; /*** Buffered output stream, write file*/DataOutputStream DataOutputStream=NewDataOutputStream (NewBufferedoutputstream (NewFileOutputStream ("/users/yangyu/downloads/compare.tmp")));  for(inti = 0; i < num; i++) {dataoutputstream.writeint (i);        } dataoutputstream.close (); intdata = 0; /*** Buffered input stream, read file*/DataInputStream in=NewDataInputStream (NewBufferedinputstream (NewFileInputStream ("/users/yangyu/downloads/compare.tmp"))); Try {             while(true) {Data=In.readint (); }        } Catch(eofexception e) {System.out.println ("Read Complete" +data);        } in.close (); LongEndTime =System.currenttimemillis (); System.out.println ("MS:" + (EndTime-startTime)); }    /*** Using NiO Bytebuffer * Time: 220ms *@throwsIOException*/@Test Public voidUseniobytebuffer ()throwsIOException {LongStartTime =System.currenttimemillis (); intnum = 2000*2000; /*** File output stream*/FileOutputStream FileOutputStream=NewFileOutputStream ("/users/yangyu/downloads/compare.tmp"); /*** NIO Channel channels*/FileChannel FileChannel=Fileoutputstream.getchannel (); /*** Bytebuffer Buffer*/Bytebuffer Buffer= Bytebuffer.allocate (num*5);  for(inti = 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=NewFileInputStream ("/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 (); LongEndTime =System.currenttimemillis (); System.out.println ("MS:" + (EndTime-startTime));        Buffer1.flip ();    System.out.println (Buffer1.limit ()); }    /*** Use Mappedbytebuffer to map files to memory by FileChannel * Time: 112ms *@throwsIOException*/@Test Public voidUserandomaccess ()throwsIOException {LongStartTime =System.currenttimemillis (); intnum = 2000*2000; /*** Use Randomaccessfile with a random access location*/randomaccessfile File=NewRandomaccessfile ("/users/yangyu/downloads/compare.tmp", "RW"); /*** Get channel channels*/FileChannel FileChannel=File.getchannel (); /*** Map files to buffers Mappedbytebuffer*/Mappedbytebuffer Mappedbytebuffer= Filechannel.map (filechannel.mapmode.read_write,0,num*4); /*** Write Files*/         for(inti = 0; i < num; i++) {mappedbytebuffer.putint (i);        } filechannel.close (); intData=0; Randomaccessfile file1=NewRandomaccessfile ("/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 (); LongEndTime =System.currenttimemillis (); System.out.println ("MS:" + (EndTime-startTime));    SYSTEM.OUT.PRINTLN (data); }}

The conclusion is very obvious, when using IO to read and write files later, use NiO mappedbytebuffer, after all, NiO is much better than the old IO performance.

Java--stream,nio Bytebuffer,nio Mappedbytebuffer Performance Comparison

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.