Java-io Operating Performance Control

Source: Internet
Author: User

In the software system. IO speeds are slower than memory, and Io reads and writes can be a bottleneck in many cases.

In Java Standard IO operations, InputStream and OutputStream provide stream-based IO operations. In bytes, reader and writer implement the buffered cache, with characters as the processing unit.

From Java1.4, add NiO (New IO), add cache buffer and channel channels, and block as the processing unit. is a bidirectional channel (readable and writable. Similar to randomaccessfile), supports lock and memory mapped file access interfaces. Greatly improves the IO speed.

The following example simply tests the performance speed of common IO operations.


/** * Test different IO operation speed * * @author Peter_wang * @create-time 2014-6-4 pm 12:52:48 */public class SpeedTest {private static F    inal String Input_file_path = "Io_speed.txt";    private static final String Output_file_path = "Io_speed_copy.txt";        /** * @param args */public static void main (string[] args) {Long ioStreamTime1 = Iostreamcopy ();        System.out.println ("IO stream copy:" + ioStreamTime1);        Long ioStreamTime2 = Bufferedstreamcopy ();        System.out.println ("Buffered stream copy:" + ioStreamTime2);        Long IoStreamTime3 = Niostreamcopy ();                System.out.println ("NiO stream copy:" + IoStreamTime3);        Long ioStreamTime4 = Niomemorystreamcopy ();    System.out.println ("NiO Memory stream copy:" + ioStreamTime4); }/** * Normal file stream read/write * * @return operation time */private static long Iostreamcopy () {Long costtime =-1        ;        FileInputStream is = null;        FileOutputStream OS = null; try {Long StartTime = System.currenttimemillis ();            is = new FileInputStream (Input_file_path);            OS = new FileOutputStream (Output_file_path);            int read = Is.read ();                while (read! =-1) {os.write (read);            Read = Is.read ();            } Long EndTime = System.currenttimemillis ();        Costtime = Endtime-starttime;        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();                } finally {try {if (is = = null) {is.close ();                } if (OS! = null) {os.close ();            }} catch (IOException e) {e.printstacktrace ();    }} return costtime; }/** * Increased cache file stream read/write, reader default implementation cache. Only can read the character file, cannot accurately read the byte file slice video and so on * * @return operation time */private static LOng Bufferedstreamcopy () {Long costtime =-1;        FileReader reader = null;        FileWriter writer = null;            try {Long startTime = System.currenttimemillis ();            reader = new FileReader (Input_file_path);            writer = new FileWriter (Output_file_path);            int read =-1;            while (read = Reader.read ())! =-1) {writer.write (read);            } writer.flush ();            Long endTime = System.currenttimemillis ();        Costtime = Endtime-starttime;        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();                } finally {try {if (reader! = null) {reader.close ();                } if (writer! = null) {writer.close (); }} catch (IOException e) {e.printstacktrace ();    }} return costtime; /** * NIO Operation Data Stream * * @return operation time */private static long Niostreamcopy () {Long costtime =        -1;        FileInputStream is = null;        FileOutputStream OS = null;        FileChannel fi = null;        FileChannel fo = null;            try {Long startTime = System.currenttimemillis ();            is = new FileInputStream (Input_file_path);            OS = new FileOutputStream (Output_file_path);            fi = Is.getchannel ();            fo = Os.getchannel ();            Bytebuffer buffer = bytebuffer.allocate (1024);                while (true) {buffer.clear ();                int read = Fi.read (buffer);                if (read = =-1) {break;                } buffer.flip ();            Fo.write (buffer);            } Long EndTime = System.currenttimemillis ();        Costtime = Endtime-starttime; } catch (FilenotfoundexceptIon e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();                } finally {try {if (fi! = null) {fi.close ();                } if (fo! = null) {fo.close ();                } if (is = null) {is.close ();                } if (OS! = null) {os.close ();            }} catch (IOException e) {e.printstacktrace ();    }} return costtime; /** * NIO Memory map Operation Data Stream * * @return operation time */private static long Niomemorystreamcopy () {long C        Osttime =-1;        FileInputStream is = null;        The map file output must be randomaccessfile randomaccessfile OS = null;        FileChannel fi = null;        FileChannel fo = null;            try {Long startTime = System.currenttimemillis (); is = NEW FileInputStream (Input_file_path);            OS = new Randomaccessfile (Output_file_path, "RW");            fi = Is.getchannel ();            fo = Os.getchannel ();            Intbuffer Iib=fi.map (FileChannel.MapMode.READ_ONLY, 0, Fi.size ()). Asintbuffer ();            Intbuffer oIb = Fo.map (FileChannel.MapMode.READ_WRITE, 0, Fo.size ()). Asintbuffer ();                while (Iib.hasremaining ()) {int read = Iib.get ();            Oib.put (read);            } Long EndTime = System.currenttimemillis ();        Costtime = Endtime-starttime;        } catch (FileNotFoundException e) {e.printstacktrace ();        } catch (IOException e) {e.printstacktrace ();                } finally {try {if (fi! = null) {fi.close ();                } if (fo! = null) {fo.close ();            } if (is = null) {is.close ();    } if (OS! = null) {os.close ();            }} catch (IOException e) {e.printstacktrace ();    }} return costtime; }}
Execution Result:

IO stream copy:384buffered stream Copy:125nio stream Copy:12nio memory stream copy:10

Conclusion Analysis:

The most common InputStream operation takes a long time. Added cache after speed added, using NIO and memory maps to access the file. Speed is the fastest.

Java-io Operating Performance Control

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.