ITeye博客

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

absolute · 2011-09-15 10:58 · Android · 移动开发 · Markdown

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

 

 

public class FileTest {

	static int length = 0x8000000; // 128 Mb

	public 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)

  1. #1 楼 · mysh · 2012-02-03 09:54

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

评论功能维护中,当前仅展示历史评论。