一、性能对比
方法 | 50M * 10 | 230M * 10 | 1G * 10 | 1.5G * 10 | 2G * 10 | 13G * 10 |
---|
nioTransferFrom() | 664 ms | 2,191ms | 9,826ms | 28,256 | 32,195ms | 266,417ms |
nioTransferTo() | 592 ms | 1,956ms | 9,834ms | 28,471ms | 32,427ms | 295,478ms |
nioByteBuffer() | 698ms | 2,611ms | 10,474ms | 15,033ms | 25,095ms | 231,016ms |
ioFileInputStream() | 1,055ms | 3,638ms | 10,474ms | 15,033ms | 25,095ms | 213,016 |
ioBuffered() | 1,100ms | 3,485ms | 14,119ms | 18,944ms | 26,274ms | 220,801ms |
二、测试代码
public static void nioTransferFrom(String inFile, String outFile) throws Exception {
long start = System.currentTimeMillis();
RandomAccessFile fromFile = new RandomAccessFile(inFile, "rw");
FileChannel inChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile(outFile, "rw");
FileChannel outChannel = toFile.getChannel();
long position = 0;
long count = inChannel.size();
inChannel.transferFrom(position, count, outChannel);
outChannel.close();
toFile.close();
inChannel.close();
fromFile.close();
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (start - end) + " ms");
}
public static void nioTransferTo(String inFile, String outFile) throws Exception {
long start = System.currentTimeMillis();
RandomAccessFile fromFile = new RandomAccessFile(inFile, "rw");
FileChannel inChannel = fromFile.getChannel();
RandomAccessFile toFile = new RandomAccessFile(outFile, "rw");
FileChannel outChannel = toFile.getChannel();
long position = 0;
long count = inChannel.size();
while(count > 0){
long current = outChannel.transferTo(position, count, inChannel);
if(current > 0) {
position += current;
count -= current;
}
}
outChannel.close();
toFile.close();
inChannel.close();
fromFile.close();
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (start - end) + " ms");
}
public static void nioByteBuffer(String inFile, String outFile) throws Exception {
long start = System.currentTimeMillis();
FileInputStream in = new FileInputStream(inFile);
FileOutputStream out = new FileOutputStream(outFile);
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 1024 * 8);
while(inChannel.read(buf) != -1) {
buf.flip();
outChannel.write(buf);
buf.clear();
}
outChannel.close();
inChannel.close();
long end = System.currentTimeMillis();
System.out.println("Time taken: "+(start - end)+" ms");
}
public static void ioFileInputStream(String inFile, String outFile) throws Exception {
long start= System.currentTimeMillis();
FileInputStream in = new FileInputStream(inFile);
FileOutputStream out = new FileOutputStream(outFile);
byte[] buf = new byte[1024 * 1024 * 8];
while(in.read(buf) != -1) {
out.write(buf, 0, len);
}
out.flush();
out.close();
in.close();
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start) + " ms");
}
public static void ioBuffered(String inFile, String outFile) throws Exception {
long start= System.currentTimeMillis();
FileInputStream inStream = new FileInputStream(inFile);
FileOutputStream outStream = new FileOutputStream(outFile);
int bufferSize = 1024 * 1024 * 8;
BufferedFileInputStream in = new BufferedFileInputStream(inStream, bufferSize);
BufferedFileOutputStream out = new BufferedFileOutputStream(outStream, bufferSize);
byte[] buf = new byte[bufferSize];
while(in.read(buf) != -1) {
out.write(buf, 0, len);
}
out.flush();
out.close();
in.close();
long end = System.currentTimeMillis();
System.out.println("Time taken: " + (end - start) + " ms");
}
三、单线程测试
public static void main(String[] args) throws Exception {
String inFile = "xxx.rar";
String outFile;
long start = System.currentTimeMillis();
for(int i = 0; i < 10; i++) {
outFile = "xxx" + i + ".rar";
nioTransferFrom(inFile, outFile);
}
long end = System.currentTimeMillis();
System.out.println("Sum: " + (start - end) + " ms");
}
四、多线程测试
public static void main(String[] args) throws Exception {
final String inFile = "xxx.rar";
final CountDownLatch startGate = new CountDownLatch(1);
final CountDownLatch endCate = new CountDownLatch(10);
for(int i = 0; i < 10; i++) {
Thread t = new Thread(){
@Override
public void run(){
try {
startGate.await();
String outFile = "xxx" + UUID.randomUUID() + ".rar";
nioTransferFrom(inFile, outFile);
} catch (Throwable e) {
e.printStackTrace();
} finally {
endCate.countDown();
}
}
};
t.start();
}
long start = System.currentTimeMillis();
System.out.println("Wwait:" + start);
startGate.countDown();
endCate.await();
long end = System.currentTimeMillis();
System.out.println("Done: " + (end - start) + " ms");
}