Java 記憶體對應檔MappedByteBuffer

來源:互聯網
上載者:User

標籤:

  •  MappedByteBuffer是java nio引入的檔案記憶體映射方案,讀寫效能極高。在NIO中主要用到普通的輸入資料流,帶緩衝的輸入資料流,RandomAccessFile和MappedByteBuffer。

現在我們來看看這四種流的效率,廢話少說直接上代碼。

我們採用CRC32來迴圈冗餘校正。CRC32在java.util.zip包中。

1.普通的輸入資料流。

 public static long checksumInputStream(Path filename) throws IOException   {      try (InputStream in = Files.newInputStream(filename))      {         CRC32 crc = new CRC32();            int c;         while ((c = in.read()) != -1)            crc.update(c);         return crc.getValue();      }   }

2.帶緩衝的輸入資料流

   public static long checksumBufferedInputStream(Path filename) throws IOException   {      try (InputStream in = new BufferedInputStream(Files.newInputStream(filename)))      {         CRC32 crc = new CRC32();            int c;         while ((c = in.read()) != -1)            crc.update(c);         return crc.getValue();      }   }

3.RandomAccessFile

 public static long checksumRandomAccessFile(Path filename) throws IOException   {      try (RandomAccessFile file = new RandomAccessFile(filename.toFile(), "r"))      {         long length = file.length();         CRC32 crc = new CRC32();            for (long p = 0; p < length; p++)         {            file.seek(p);            int c = file.readByte();            crc.update(c);         }         return crc.getValue();      }   }

4.MappedByteBuffer

   public static long checksumMappedFile(Path filename) throws IOException   {      try (FileChannel channel = FileChannel.open(filename))      {         CRC32 crc = new CRC32();         int length = (int) channel.size();         MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);            for (int p = 0; p < length; p++)         {            int c = buffer.get(p);            crc.update(c);         }         return crc.getValue();      }   }

每個流對應的方法寫好之後我們來開始測試

public static void main(String[] args) throws IOException {        String name = "txt.txt";        Path filename = Paths.get(name);        long start, crcValue, end;        System.out.println("Input Stream:");        start = System.currentTimeMillis();        crcValue = checksumInputStream(filename);        end = System.currentTimeMillis();        System.out.println(Long.toHexString(crcValue));        System.out.println((end - start) + " milliseconds");        System.out.println("Buffered Input Stream:");        start = System.currentTimeMillis();        crcValue = checksumBufferedInputStream(filename);        end = System.currentTimeMillis();        System.out.println(Long.toHexString(crcValue));        System.out.println((end - start) + " milliseconds");        System.out.println("Random Access File:");        start = System.currentTimeMillis();        crcValue = checksumRandomAccessFile(filename);        end = System.currentTimeMillis();        System.out.println(Long.toHexString(crcValue));        System.out.println((end - start) + " milliseconds");        System.out.println("Mapped File:");        start = System.currentTimeMillis();        crcValue = checksumMappedFile(filename);        end = System.currentTimeMillis();        System.out.println(Long.toHexString(crcValue));        System.out.println((end - start) + " milliseconds");    }

  為了保證每種流相互之間對結果沒有影響,我們可以分別把main方法中其他流的代碼注釋掉。

     最後可以看到MappedByteBuffer效率最高,所消耗的時間最少。

Java 記憶體對應檔MappedByteBuffer

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.