• Java之nio性能比较


    结果:
    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();

    }
    }

  • 相关阅读:
    在GDI+中如何实现以左下角为原点的笛卡尔坐标系
    html中内联元素和块元素的区别、用法以及联系
    HttpClient超时设置
    springMVC实现文件上传
    IDEA生成serialVersionUID的警告
    java中两个字符串如何比较大小
    gerrit简版教程
    mysql中OPTIMIZE TABLE的作用及使用
    mysql慢查询日志分析
    checkStype和findBugs校验
  • 原文地址:https://www.cnblogs.com/zolo/p/5849323.html
Copyright © 2020-2023  润新知