零拷贝速度测试

文件大小:1.46 GB (1,568,946,426 字节)

  • 使用transferTo零拷贝,代码如下,耗时12116ms
        public static void main(String[] args) throws IOException {
            long start=System.currentTimeMillis();
            FileChannel inputChannel=new FileInputStream(new File("E:\\video\\第1阶段.zip")).getChannel();
            FileChannel outChannel=new FileOutputStream(new File("E:\\video\\第1阶段1.zip")).getChannel();
            inputChannel.transferTo(0,inputChannel.size(),outChannel);
            System.out.println(System.currentTimeMillis()-start);
        }
  • 使用阻塞io,代码如下,耗时19464ms,代码如下:
        public static void main(String[] args) throws IOException {
            long start=System.currentTimeMillis();
            FileInputStream inputstream=new FileInputStream(new File("E:\\video\\第1阶段.zip"));
            FileOutputStream  outStream=new FileOutputStream(new File("E:\\video\\第1阶段1.zip"));
            int bytesWritten = 0;
            int byteCount = 0;
            byte[] bytes = new byte[1024];
            while ((byteCount = inputstream.read(bytes)) != -1)
            {
                outStream.write(bytes,0, byteCount);
            }
            inputstream.close();
            outStream.close();
            System.out.println(System.currentTimeMillis()-start);
        }

    性能提升:37.75%

上一篇:2021-07-30


下一篇:String字符串连接性能优化StringBuilder(阿里Java开发手册规范)