Java Io Performance Testing

Source: Internet
Author: User

In JDK 1.4, the NIO package is added, which is aimed at improving the IO speed. However, we all know that with the NIO package added, the old Io package is actually overwritten. Even if the NIO package is not displayed, You can obviously feel the speed improvement.

In addition, many people only know the inputstream or outputstream of the buffer when using the IO package, and the speed will be faster.

So, is there a gap in speed between the above? What is the gap? We will perform an I/O operation performance test.


I/O operations are as follows: normal file read/write (WITHOUT BUFFER), file read/write with buffer, normal file read/write using NiO pipeline, and random file read/write using NiO pipeline.


Write a testio test class first.

/*** Test Io abstract class * @ author wing * @ date 2012/7/22 */public abstract class testio {private long start, time; Public void testtime () {start = system. nanotime (); test (); time = system. nanotime ()-start; system. out. format ("%. 3f \ n ", time/1.0e9);} protected abstract void test ();}

The operation time used to test the test method.


Then there is the fileio class for various Io operations.

 

Package Org. wing. NIO. test; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. datainputstream; import Java. io. dataoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. randomaccessfile; import Java. NIO. intbuffer; import Java. NIO. channels. filechannel; Import Java. NIO. channels. filechannel. mapmode;/*** Io and NiO File Operations * @ author wing * @ date 2012/7/22 */public class fileio {Private Static final int COUNT = 400000; private Static final int buffersize = 400000;/*** write the file to the output stream without using the buffer package */public static void writeio (string filename) {dataoutputstream mdos = NULL; try {mdos = new dataoutputstream (New fileoutputstream (new file (filename); For (INT I = 0; I <count; I ++) {mdos. writeint (1) ;}} catch (filenotfoundexception e) {system. out. println ("file not found:" + E. tostring ();} catch (ioexception e) {system. out. println ("Io exception:" + E. tostring ();} finally {If (mdos! = NULL) {try {mdos. close ();} catch (ioexception e) {system. out. println ("stream close exception:" + E. tostring () ;}}}/ *** do not use buffer to wrap the input stream to read the file */public static void readio (string filename) {datainputstream MDIs = NULL; try {MDIs = new datainputstream (New fileinputstream (new file (filename); For (INT I = 0; I <count; I ++) {MDIs. readint () ;}} catch (filenotfoundexception e) {system. out. println ("file n Ot found: "+ E. tostring ();} catch (ioexception e) {system. out. println ("Io exception:" + E. tostring ();} finally {If (MDIs! = NULL) {try {MDIs. close ();} catch (ioexception e) {system. out. println ("stream close exception:" + E. tostring () ;}}}/*** use the buffer-encapsulated output stream to write the file */public static void writeiowithbuffer (string filename) {dataoutputstream mdos = NULL; try {mdos = new dataoutputstream (New bufferedoutputstream (New fileoutputstream (new file (filename); For (INT I = 0; I <count; I ++) {mdos. writeint (1) ;}} catch (filenotfou Ndexception e) {system. out. println ("file not found:" + E. tostring ();} catch (ioexception e) {system. out. println ("Io exception:" + E. tostring ();} finally {If (mdos! = NULL) {try {mdos. close ();} catch (ioexception e) {system. out. println ("stream close exception:" + E. tostring () ;}}}/ *** use the buffer to package the input stream to read the file */public static void readiowithbuffer (string filename) {datainputstream MDIs = NULL; try {MDIs = new datainputstream (New bufferedinputstream (New fileinputstream (new file (filename); For (INT I = 0; I <count; I ++) {MDIs. readint () ;}} catch (filenotfoundexcept Ion e) {system. out. println ("file not found:" + E. tostring ();} catch (ioexception e) {system. out. println ("Io exception:" + E. tostring ();} finally {If (MDIs! = NULL) {try {MDIs. close ();} catch (ioexception e) {system. out. println ("stream close exception:" + E. tostring () ;}}}/*** use the NIO pipeline to write data */public static void writenio (string filename) {filechannel MFC = NULL; try {MFC = new fileoutputstream (new file (filename )). getchannel (); intbuffer mbuffer = intbuffer. allocate (buffersize); For (INT I = 0; I <count; I ++) {mbuffer. put (1) ;}} catch (filenotfoundexce Ption e) {system. Out. println ("file not found:" + E. tostring () ;}finally {If (MFC! = NULL) {try {MFC. close ();} catch (ioexception e) {system. out. println ("channel close exception:" + E. tostring () ;}}}/ *** use the NIO pipeline to read data */public static void readnio (string filename) {filechannel MFC = NULL; try {MFC = new fileinputstream (new file (filename )). getchannel (); intbuffer mbuffer = intbuffer. allocate (buffersize); For (INT I = 0; I <count; I ++) {mbuffer. get () ;}} catch (filenotfoundexcep Tion E) {system. Out. println ("file not found:" + E. tostring ();} finally {If (MFC! = NULL) {try {MFC. close ();} catch (ioexception e) {system. out. println ("channel close exception:" + E. tostring () ;}}}/*** use the NIO pipeline to write data */public static void writeniowithran (string filename) {filechannel MFC = NULL; try {MFC = new randomaccessfile (filename, "RW "). getchannel (); intbuffer mbuffer = MFC. map (mapmode. read_write, 0, 4 * buffersize ). asintbuffer (); For (INT I = 0; I <count; I ++) {mbuffer. Put (1) ;}} catch (filenotfoundexception e) {system. out. println ("file not found:" + E. tostring ();} catch (ioexception e) {system. out. println ("Io exception:" + E. tostring ();} finally {If (MFC! = NULL) {try {MFC. close ();} catch (ioexception e) {system. out. println ("channel close exception:" + E. tostring () ;}}}/*** use the NIO pipeline to read data */public static void readniowithran (string filename) {filechannel MFC = NULL; try {MFC = new randomaccessfile (filename, "RW "). getchannel (); intbuffer mbuffer = MFC. map (mapmode. read_write, 0, 4 * buffersize ). asintbuffer (); For (INT I = 0; I <count; I ++) {mbuffer. g Et () ;}} catch (filenotfoundexception e) {system. out. println ("file not found:" + E. tostring ();} catch (ioexception e) {system. out. println ("Io exception:" + E. tostring ();} finally {If (MFC! = NULL) {try {MFC. close ();} catch (ioexception e) {system. out. println ("channel close exception:" + E. tostring ());}}}}}

As you can see, there are various Io operations. The number of read/write operations is 400000 for the int type.


Next is our main class.


package org.wing.nio.test;public class MainClass {public static void main(String[] args) {final String fileName = "test";TestIO[] testList = new TestIO[] {    new TestIO() {@Overrideprotected void test() {FileIO.writeIO(fileName);}}, new TestIO() {@Overrideprotected void test() {FileIO.readIO(fileName);}}, new TestIO() {@Overrideprotected void test() {FileIO.writeIOWithBuffer(fileName);}}, new TestIO() {@Overrideprotected void test() {FileIO.readIOWithBuffer(fileName);}}, new TestIO() {@Overrideprotected void test() {FileIO.writeNIO(fileName);}}, new TestIO() {@Overrideprotected void test() {FileIO.readNIO(fileName);}},new TestIO() {@Overrideprotected void test() {FileIO.writeNIOWithRan(fileName);}}, new TestIO() {@Overrideprotected void test() {FileIO.readNIOWithRan(fileName);}},};for(TestIO testIO : testList){testIO.testTime();}}}

Create an array of testio classes, rewrite the test method, and use your own Io operations. Then, execute the testtime method of testio cyclically.


Run the program and check the result.


As you can see, in Io operations without using buffer decoration, 400000 int write operations took 5.764 seconds and 3.301 read operations took seconds. I/O operations decorated with buffer read 0.021 seconds and write 0.020 seconds. However, Io operations on common files and random read/write files are directly performed using NiO pipelines, which takes less time, write operations only 0.010 seconds, and read operations only 0.006 seconds.


In the first test, we can see that the IO operations decorated with buffer far exceed the IO operations not decorated with buffer. In 400000 operations, the pipeline read and write speed of the NIO package was almost the same.


So is the I/O package's Io read/write speed similar to that of NiO?

We will perform the next round of testing.


Comment out and create the testio class without using the buffer decoration. Because we need to increase the number of Io operations, Io operations without using the buffer decoration will be extremely time-consuming.

We changed the number of Io operations to 4000000.

Run the command to view the result.



As you can see, in the 4000000 Io operations, the read and write time of the buffer's common Io operations is about 0.6 seconds, while the pipeline's Io operations are used, and the read and write time is about 0.05 seconds, the I/O operation read/write time of the pipeline that uses random access to files is about 0.065 seconds.


Then, we continue to increase the number of Io operations to 40000000.

Run the program and check the result.


As you can see, after increasing the number of I/O operations, I/O operations with buffer in the I/O package took up to 5.8 seconds, while the read operation reached 1.7 seconds !! However, Io operations using the NIO Package Pipeline only take 0.33 seconds for write operations and 0.27 seconds for read operations. I/O operations on pipelines that use random access to files take a little longer. Write operations take 0.57 seconds and read operations take 0.4 seconds.


It can be seen that even if I/O packages are rewritten after JDK 1.4, the efficiency is improved. However, after a certain number of I/O operations, the pipeline operation efficiency gap between the I/O package and NiO package is getting bigger and bigger. The general I/O operations that use random access to files consume a little longer, but are not significantly affected. However, I didn't perform the seek operation here, and then read and write. It is estimated that the efficiency will be greatly affected.


Of course, in our daily development, I/O operations that use the IO package to decorate the buffer are enough. However, this also shows the strength and efficiency of the NIO package.


The test may be incorrect in many places, but there should be no major problems as a whole. Let's take a look.

Reprinted please indicate the source: http://blog.csdn.net/ml3947

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.