日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

NIO中的內存映射文件使用對效率提高的驗證

系統 2216 0

?對比較大的而不能放入內存的文件進行I/O操作時,如果使用NIO中的內存映射文件對性能效率和速度的提高是非常顯著的。首先需要獲取文件的通道,然后調用通道的map(FileChannel.MapMode mode,long position,long size)函數將文件從position位置開始的長度為size的內容映射到內存中。具體的效率比較代碼示例如下:

?

Java代碼?? 收藏代碼
  1. import ?java.io.BufferedInputStream;??
  2. import ?java.io.BufferedOutputStream;??
  3. import ?java.io.DataInputStream;??
  4. import ?java.io.DataOutputStream;??
  5. import ?java.io.FileInputStream;??
  6. import ?java.io.FileOutputStream;??
  7. import ?java.io.IOException;??
  8. import ?java.io.RandomAccessFile;??
  9. import ?java.nio.IntBuffer;??
  10. import ?java.nio.channels.FileChannel;??
  11. ??
  12. //測試代碼是借用thingking?in?java中的 ??
  13. public ? class ?MappedIO?{??
  14. ??????
  15. ???? //先聲明需要寫入的文件的內容的大小 ??
  16. ???? private ? static ? final ? int ?NUMOFINT= 4000000 ;??
  17. ???? private ? static ? final ? int ?NUMOFOUT= 2000000 ;??
  18. ??????
  19. ???? //測試類,用來測試使用普通的I/O操作和使用內存文件映射時速度的差別 ??
  20. ???? abstract ? static ? class ?Tester{??
  21. ???????? private ?String?name;??
  22. ???????? public ?Tester(String?name){??
  23. ???????????? this .name=name;??
  24. ????????}??
  25. ??????????
  26. ???????? public ? void ?runTest(){??
  27. ????????????System.out.println( "使用" +name+ "所消耗的時間:" );??
  28. ???????????? try {??
  29. ???????????? //以毫微秒為單位獲取測試函數開始前的時間 ??
  30. ???????????? long ?begin=System.nanoTime();??
  31. ??????????????
  32. ????????????test();??
  33. ???????????? //將測試函數結束后的系統的時間減去開始之前的時間獲取 ??
  34. ???????????? double ?duration=System.nanoTime()-begin;??
  35. ???????????? //PrintStream中的使用指定格式字符串和參數將格式化字符串寫入此輸出流中的方法 ??
  36. ????????????System.out.format( "%.2f\n" ,?duration/ 1 .0e9);??
  37. ????????????} catch (IOException?e){??
  38. ????????????????e.printStackTrace();??
  39. ????????????}??
  40. ????????}??
  41. ??????????
  42. ???????? //具體的測試函數中其實不僅有對文件進行讀取或寫入的時間,還包括消耗的各種初始化I/O對象的時間,而且內存映射文件的初始化對象消耗更要比普通的操作大 ??
  43. ???????? public ? abstract ? void ?test()? throws ?IOException;??
  44. ????}??
  45. ??????
  46. ???? private ? static ?Tester[]?tests={??
  47. ???????? //先使用普通的stream?write,并且還是用了Buffered緩沖 ??
  48. ???????? new ?Tester( "stream?write" ){??
  49. ???????????? public ? void ?test() throws ?IOException{??
  50. ????????????????DataOutputStream?dos= new ?DataOutputStream( new ?BufferedOutputStream( new ?FileOutputStream( "baolei.txt" )));???
  51. ???????????????? for ( int ?i= 0 ;i<NUMOFINT;i++){??
  52. ????????????????????dos.writeInt(i);??
  53. ????????????????}??
  54. ????????????????dos.close();??
  55. ????????????}??
  56. ????????},??
  57. ???????? //使用內存映射文件方式寫入文件時的測試內部類 ??
  58. ???????? new ?Tester( "mapped?write" ){??
  59. ???????????? public ? void ?test()? throws ?IOException{??
  60. ???????????????? //利用RandomAccessFile初始化文件獲取通道 ??
  61. ????????????????FileChannel?fc= new ?RandomAccessFile( "baolei.txt" , "rw" ).getChannel();??
  62. ???????????????? //利用IntBuffer基本類型視圖緩沖器來修改底層的ByteBuffer,實際上ByteBuffer是將數據移進移出通道的唯一方式 ??
  63. ????????????????IntBuffer?ib=fc.map(FileChannel.MapMode.READ_WRITE,? 0 ,fc.size()).asIntBuffer();??
  64. ???????????????? for ( int ?i= 0 ;i<NUMOFINT;i++){??
  65. ????????????????????ib.put(i);??
  66. ????????????????}??
  67. ??????????????????
  68. ????????????????fc.close();??
  69. ????????????}??
  70. ????????},??
  71. ??????????
  72. ???????? //下面的兩個測試方法是測試read文件時的速度,基本和上面的兩個一樣 ??
  73. ???????? new ?Tester( "stream?read" ){??
  74. ???????????? public ? void ?test() throws ?IOException{??
  75. ????????????????DataInputStream?dis= new ?DataInputStream( new ?BufferedInputStream( new ?FileInputStream( "baolei.txt" )));??
  76. ??????????????????
  77. ???????????????? for ( int ?i= 0 ;i<NUMOFOUT;i++){??
  78. ????????????????????dis.readInt();??
  79. ????????????????}??
  80. ????????????????dis.close();??
  81. ????????????}??
  82. ????????},??
  83. ???????? new ?Tester( "mapped?read" ){??
  84. ???????????? public ? void ?test()? throws ?IOException{??
  85. ????????????????FileChannel?fc= new ?RandomAccessFile( "baolei.txt" , "rw" ).getChannel();??
  86. ????????????????IntBuffer?ib=fc.map(FileChannel.MapMode.READ_WRITE,? 0 ,?fc.size()).asIntBuffer();??
  87. ???????????????? while (ib.hasRemaining()){??
  88. ????????????????????ib.get();??
  89. ????????????????}??
  90. ??????????????????
  91. ????????????????fc.close();??
  92. ????????????}??
  93. ????????}??
  94. ??
  95. ????};??
  96. ??????
  97. ???? public ? static ? void ?main(String[]?args){??
  98. ???????? for (Tester?test:tests){??
  99. ????????????test.runTest();??
  100. ????????}??
  101. ????}??
  102. ??
  103. }??

?

?? 可以看到運行后的結果如下:

????????? ?使用stream write所消耗的時間:
?????????? 0.92
????????? 使用mapped write所消耗的時間:
?????????? 0.12?
????????? 使用stream read所消耗的時間:
?????????? 0.50
???????? 使用mapped read所消耗的時間:
?????????? 0.06
??? 效率確實大幅度提高啊。

NIO中的內存映射文件使用對效率提高的驗證


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 松滋市| 大厂| 泾阳县| 即墨市| 乌恰县| 五大连池市| 丰县| 水富县| 蒙阴县| 安陆市| 石首市| 崇义县| 湘乡市| 恩施市| 冷水江市| 牙克石市| 永修县| 石河子市| 紫云| 新化县| 庆阳市| 博爱县| 磴口县| 海阳市| 哈尔滨市| 文昌市| 温泉县| 平度市| 阆中市| 天津市| 治县。| 衡南县| 河源市| 蕲春县| 永州市| 仪征市| 沛县| 泊头市| 扎兰屯市| 合作市| 石首市|