结果:
used time:53574684
used time:1800077620
used time:12563690
可见MappedByteBuffer读写数据是最快的, 其次是FileChannel, 再其次就是RandomAccessFile.
public class BufferedCache {

/**
  * @param args
  * @throws IOException
  */
public static void main(String[] args) throws IOException {
  test3();
  test2();
  test1();
}

public static void test1() throws IOException {
  long start = System.nanoTime();
  FileChannel fc = new RandomAccessFile("/tmp/test.tmp", "rw").getChannel();
  MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 8);

for (int i = 0; i < 100000; i++) {
   mbb.putLong(0, i);
  }
  long end = System.nanoTime();
  System.out.println("used time:" + (end - start));
  fc.close();

}

public static void test2() throws IOException {
  long start = System.nanoTime();
  RandomAccessFile raf = new RandomAccessFile("/tmp/test.tmp", "rw");
  for (int i = 0; i < 100000; i++) {
   raf.seek(0);
   raf.writeLong(i);
  }
  long end = System.nanoTime();
  System.out.println("used time:" + (end - start));
  raf.close();
}

public static void test3() throws IOException {
  long start = System.nanoTime();
  FileChannel fc = new RandomAccessFile("/tmp/test.tmp", "rw").getChannel();
  ByteBuffer buf = ByteBuffer.allocate(8);
  for (int i = 0; i < 100000; i++) {
   buf.putLong(0, i);
   fc.write(buf, 0);
  }
  long end = System.nanoTime();
  System.out.println("used time:" + (end - start));
  fc.close();

}
}

最新文章

  1. Exchange环境搭建心得
  2. crontab中执行任务定位到秒级
  3. 使用ContentProvider进行应用程序间的数据交互
  4. X-UA-Compatible/IE=EmulateIE7/IE=7
  5. 洛谷P2014 选课 (树形dp)
  6. 【洛谷P3398】仓鼠找sugar
  7. ch2 创建和销毁对象
  8. composer autoload
  9. java提高篇---HashTable
  10. VMware 进入bios
  11. kail新手安装
  12. poj 2104 划分树
  13. C语言 打印圣诞树
  14. 由问题引出的fsck命令
  15. Jerry的UI5框架代码自学教程
  16. 【题解】P1119 灾后重建
  17. JAVA ArrayList实现随机生成数字,并把偶数放入一个列表中
  18. [书籍]用UWP复习《C#并发编程经典实例》
  19. Linux的SIGUSR1和SIGUSR2信号
  20. SSD详解

热门文章

  1. 使用Perl批量读取文件最后行
  2. 用live555将内网摄像机视频推送到外网server,附源代码
  3. iOS开发ARC与MRC下单例的完整写法与通用宏定义
  4. Loader之一:基本原理 分类: H1_ANDROID 2013-11-16 10:29 1923人阅读 评论(0) 收藏
  5. 监听text等的改变事件
  6. &lt;Linux&gt; Xen虚拟机镜像的安装
  7. ios 第一篇文章-xcode6.2键盘调不出来
  8. 【codeforces 757B】 Bash's Big Day
  9. Vert.x ——概述
  10. 剑指Offer面试题10(Java版):二进制中的1的个数