读书人

内存储器映射文件性能对比测试

发布时间: 2012-06-28 15:20:04 作者: rapoo

内存映射文件性能对比测试

今天对比了一下内存映射文件的性能和普通文件的测试,不比不知道,一比吓一跳啊。差距太大了。

?

?

public class FileTest {static int length = 0x8000000; // 128 Mbpublic void doMemTest() {try {long start = System.currentTimeMillis();FileChannel fc = new RandomAccessFile("e:/test/test.dat", "rw").getChannel();MappedByteBuffer out = fc.map(FileChannel.MapMode.READ_WRITE, 0, length);for (int i = 0; i < length; i++) {              out.put((byte) 'x');          } long end = System.currentTimeMillis();fc.close();System.out.println(end - start);} catch (Exception e) {e.printStackTrace();}}public void doGeneralTest() {try {long start = System.currentTimeMillis();RandomAccessFile fc = new RandomAccessFile("e:/test/test.dat", "rw");for (int i = 0; i < length; i++) {              fc.write((byte) 'x');        } long end = System.currentTimeMillis();System.out.println(end - start);fc.close();} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) throws Exception {FileTest t = new FileTest();//t.doMemTest();t.doGeneralTest();}}

?

?

doMemtest这个方法,1000多毫秒的样子,doGeneralTest这个基本就没跑完过。

1 楼 mysh 2012-02-03 这个不能说明内存映射比较快吧, 只能说明 nio 比 io 快, 要比较的话起码两个都用直接写数据块的方式, put(byte[]) write(byte[])

读书人网 >移动开发

热点推荐