Java file operations and caching mechanisms still have bottlenecks

Source: Internet
Author: User

When we use Windows system, we find that the speed of copying a file is generally consistent, we can guess that this is a Microsoft internal staff design an algorithm, and this operation can achieve the maximum speed is what effect??

Then can we design a method to replace Windows with the copy and paste method to achieve faster transmission speed?

The following code implements the file copy work in a buffered situation:

Publicvoid copybyte (String srcfile,string destfile) throws ioexception{
    
    //Create input stream
    
    InputStream ins=new FileInputStream (srcfile);
    
    Create output stream
    
    outputstream outs=new fileoutputstream (destfile);
    
    Create buffer, read 16K file
    
    byte[] buf=newbyte[16384];
    
    int I=ins.read (BUF);
    
    SYSTEM.OUT.PRINTLN ("Start copying ...");
    
    Gets the current time
    
    System.out.println (System.currenttimemillis ());
    
    while (i!=-1) {
    
      outs.write (BUF);
    
      I=ins.read (BUF);
    
    }
    
    System.out.println (System.currenttimemillis ());
    
    System.out.println ("Copy over ...");
    
    Close Flow
    
    ins.close ();
    
    Outs.close ();
    
}

First of all, I used a program of more than 860 m to test, found that the speed of copying and pasting in Windows is roughly 40M per second, copying one such file takes approximately more than 20 seconds, while using the above method to replicate, after repeated testing, Found that the time is basically about 23 seconds in appearance, and when the value of more than 16K, the speed will not be increased, and adjust to 8K, time is about 29 seconds, in fact, this also explains a phenomenon, the algorithm itself can not exceed the computer hard disk and CPU interaction speed, the hardware is still the bottleneck, But can we use an external faster hard drive to achieve the speed of the original hard drive??

In addition, see the Bufferredinputstream source of all know that there is such a property:

Privatestaticintdefaultbuffersize = 8192;

This means that the buffer input stream defined buffer array size is 8K, and did not reach the computer's limit speed, the computer has more secrets waiting for us to find out?

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

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.