---
title: "内存映射文件性能对比测试"
author: "absolute"
date: "2011-09-15T10:58:28Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-09-15T10:58:28Z"
id: 1172328
url: "https://absolute.iteye.com/blog/1172328"
---

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

 

 

```
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这个基本就没跑完过。