使用 linux 上使用 strace 命令获得系统调用
下面这个会先拷贝到用户空间
public void test2() throws IOException { // 220
long start = System.currentTimeMillis();
try (
FileInputStream inputStream = new FileInputStream("rop.mp4");
FileChannel inChannel = inputStream.getChannel();
FileOutputStream outputStream = new FileOutputStream("rop2.mp4");
FileChannel outChannel = outputStream.getChannel();
) {
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
while (inChannel.read(byteBuffer) != -1) {
byteBuffer.flip();
outChannel.write(byteBuffer);
byteBuffer.clear();
}
}
System.out.println(System.currentTimeMillis() - start);
}
下面这个是零拷贝
public void test5() throws IOException { // 29
long start = System.currentTimeMillis();
try (
FileChannel inChannel = FileChannel.open(Paths.get("rop.mp4"), StandardOpenOption.READ);
FileChannel outChannel = FileChannel.open(Paths.get("rop2.mp4"), StandardOpenOption.WRITE, StandardOpenOption.READ, StandardOpenOption.CREATE)
) {
//inChannel.transferTo(0, inChannel.size(), outChannel);
outChannel.transferFrom(inChannel, 0, inChannel.size());
}
System.out.println(System.currentTimeMillis() - start);
}
来源:oschina
链接:https://my.oschina.net/u/3870422/blog/4268247