NIO的性能测试
原来的io已经用新io重新实现过。对于读写文件,看到很多人都推荐使用new io,说什么速度快。我做一下测试。
本测试读写的文件的大小是2M字节多一点。也可看http://blog.csdn.net/jdgdf566/article/details/17079067
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.Paths;
/**
* @ 测试结论:只要缓冲大小设置得当,使用旧io和基本类型写的IO是最快的。
* @author jdgdf566
*/
public class NewMain {
private static int commonIObuffer = 1024 * 32;
/**
* nio内存映射
*
* @throws IOException
*/
public static void mapChannel() throws IOException {
FileInputStream in = new FileInputStream("Java NIO (JSR 51 NIO.1)中文版.pdf");
long size = in.available();
RandomAccessFile out = new RandomAccessFile(Double.toString(Math.random()), "rw");
FileChannel inc = in.getChannel();
MappedByteBuffer bf = inc.map(FileChannel.MapMode.READ_ONLY, 0, size);
FileChannel outc = out.getChannel();
MappedByteBuffer outbf = outc.map(FileChannel.MapMode.READ_WRITE, 0, size);
outbf.put(bf);
inc.close();
outc.close();
in.close();
out.close();
}
/**
* 原io
*
* @throws IOException
*/
public static void commonIO() throws Exception {
NewMain.fileToStream("Java NIO (JSR 51 NIO.1)中文版.pdf", new FileOutputStream(Double.toString(Math.random())));
}
public static int fileToStream(String path, OutputStream out) throws Exception {
int bufferSize = commonIObuffer;
FileInputStream fis = new FileInputStream(path);
//ba文件写入liu
int dataSize = 0;
int len;
byte[] bs = new byte[bufferSize];
while ((len = fis.read(bs)) != -1) {
out.write(bs, 0, len);
dataSize += len;
}
out.flush();
return dataSize;
}
/**
* nio
*
* @throws IOException
*/
public static void channel_transferTo() throws Exception {
String path = "F:\\NetBeansProjects\\JavaApplicationTest\\src\\Java NIO (JSR 51 NIO.1)中文版.pdf";
//String outpath = "F:\\NetBeansProjects\\JavaApplicationTest\\src/0.2974592999005251";
FileChannel fileChannel = FileChannel.open(Paths.get(path));
fileChannel.transferTo(0, fileChannel.size(), Channels.newChannel(new BufferedOutputStream(new FileOutputStream(Double.toString(Math.random())), commonIObuffer)));
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
// TODO code application logic here
long t1 = System.currentTimeMillis();
//NewMain.mapChannel();
//NewMain.commonIO();
//NewMain.channel_transferTo();
long t2 = System.currentTimeMillis();
System.out.println(t2 - t1);
}
}
测试结论:只要缓冲大小设置得当,使用旧io和基本类型写的IO是最快的。
[解决办法]
我暂时没有空实验,不过看上去NIO用的是随机读写,旧IO用的是顺序读写,两者本来就不能比较。你用FileChannel试试看,速度应该差不多的
[解决办法]
请弄个2G的文件去实验
另外还要注意一下JVM内存
NIO使用的JVM内存很有意思
[解决办法]
文件读写体现不了nio的优势。在使用socket高并发网络通信的时候,你就会知道nio比io的性能高很多。
[解决办法]
学习了...以前看过 NIO的书现在也快忘了.
[解决办法]
文件的io和nio读写速度没有太大区别。
[解决办法]
nio主要还是在网络io这一块对性能的提升比较明显,是来自于线程模型的进步,这一点是毋庸置疑的
另外文件io的话,我记得nio里好像有一个方法可以把小文件map到内存来操作,这个api旧io里好像是没有的,这个api对性能的帮助也比较大
[解决办法]
额,,,我说的内存不是指内存映射,,,,我是说NIO使用的内存很有意思的,,你可以研究一下,,这也是为什么用NIO处理大文件的一个点
[解决办法]
NIO不是为了快,特别是单线程环境中测试根本没有意义。