• 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();

    }
    }

  • 相关阅读:
    皇帝的用人之道,这一点古今皆同
    sharepoint打包
    powershellbegin
    taxonomy
    powershelluninstall webapplication
    面试题
    字符串处理
    在页面中插入视频时的文件夹命名问题
    process object
    扩展名显示与隐藏
  • 原文地址:https://www.cnblogs.com/zolo/p/5849323.html
Copyright © 2020-2023  润新知