Skip to content

Instantly share code, notes, and snippets.

@sqtds
Created August 3, 2015 02:15
Show Gist options
  • Select an option

  • Save sqtds/816b4e9064d605eca30d to your computer and use it in GitHub Desktop.

Select an option

Save sqtds/816b4e9064d605eca30d to your computer and use it in GitHub Desktop.
测试io与nio性能
package visu.platform.analyzer;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* <p>类说明:</p>
*
* @version 1.0
* <p>文件名:Iotest</p>
* <p>创建人及时间: suntiancheng 2015/8/2</p>
* <p/>
* <p>修改人:</p>
* <p>修改时间:</p>
* <p>修改描述:</p>
*/
public class IoTest {
/** 186M文件
* 采用传统IO InputStream 读取,耗时:442
采用传统IO BufferedInputStream 读取,耗时:584
采用传统IO RandomAccessFile 读取,耗时:745
采用NIO FileChannel 自带方法 读取,耗时:387
采用NIO FileChannel 循环 读取,耗时:479
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File file = new File("H:\\visu20150515.zip");
File file1 = new File("H:\\visu20150516.zip");
System.out.println(file.getTotalSpace());
copyFile(file, file1);
file1.delete();
copyFile1(file, file1);
file1.delete();
copyFile3(file, file1);
file1.delete();
copyFile4(file, file1);
file1.delete();
copyFile5(file, file1);
}
public static void copyFile(final File from, final File to) throws IOException {
long beginTime = System.currentTimeMillis();
final InputStream is = new FileInputStream(from);
final OutputStream os = new FileOutputStream(to);
final byte[] buf = new byte[8192];
int read = 0;
while ((read = is.read(buf)) != -1) {
os.write(buf, 0, read);
}
os.flush();
os.close();
is.close();
System.out.println("采用传统IO InputStream 读取,耗时:" + (System.currentTimeMillis() - beginTime));
}
public static void copyFile1(final File from, final File to) throws IOException {
long beginTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(from);
FileOutputStream fos = new FileOutputStream(to);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[8192];
int len = 0;
while ((len = bis.read(b)) != -1) {
bos.write(b, 0, len);
}
bos.flush();
fis.close();
fos.close();
bis.close();
bos.close();
System.out.println("采用传统IO BufferedInputStream 读取,耗时:" + (System.currentTimeMillis() - beginTime));
}
public static void copyFile3(final File from, final File to) throws IOException {
long beginTime = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(from);
RandomAccessFile raf = new RandomAccessFile(to, "rw");
byte[] b = new byte[8192];
int len = 0;
while ((len = fis.read(b)) != -1) {
raf.write(b, 0, len);
}
raf.close();
fis.close();
System.out.println("采用传统IO RandomAccessFile 读取,耗时:" + (System.currentTimeMillis() - beginTime));
}
public static void copyFile4(final File from, final File to) throws IOException {
long beginTime = System.currentTimeMillis();
FileChannel fc = new FileInputStream(from).getChannel();
FileChannel fco = new FileOutputStream(to)
.getChannel();
fco.transferFrom(fc, 0, fc.size());
fco.close();
fc.close();
System.out.println("采用NIO FileChannel 自带方法 读取,耗时:" + (System.currentTimeMillis() - beginTime));
}
public static void copyFile5(final File from, final File to) throws IOException {
long beginTime = System.currentTimeMillis();
FileChannel fc = new FileInputStream(from).getChannel();
FileChannel fco = new FileOutputStream(to).getChannel();
ByteBuffer buf = ByteBuffer.allocate(8192);
while (fc.read(buf) != -1) {
buf.flip();
fco.write(buf);
buf.clear();
}
System.out.println("采用NIO FileChannel 循环 读取,耗时:" + (System.currentTimeMillis() - beginTime));
}
}
@sqtds
Copy link
Author

sqtds commented Aug 3, 2015

1GB
采用传统IO InputStream 读取,耗时:10694
采用传统IO BufferedInputStream 读取,耗时:9426
采用传统IO RandomAccessFile 读取,耗时:9223
采用NIO FileChannel 自带方法 读取,耗时:9764
采用NIO FileChannel 循环 读取,耗时:9213

@sqtds
Copy link
Author

sqtds commented Aug 3, 2015

5.39 GB
采用传统IO InputStream 读取,耗时:53357
采用传统IO BufferedInputStream 读取,耗时:51795
采用传统IO RandomAccessFile 读取,耗时:51041
采用NIO FileChannel 自带方法 读取,耗时:51221
采用NIO FileChannel 循环 读取,耗时:56318

@sqtds
Copy link
Author

sqtds commented Aug 3, 2015

186M
采用传统IO InputStream 读取,耗时:442
采用传统IO BufferedInputStream 读取,耗时:584
采用传统IO RandomAccessFile 读取,耗时:745
采用NIO FileChannel 自带方法 读取,耗时:387
采用NIO FileChannel 循环 读取,耗时:479

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment