1. 程式人生 > >evilcry2012的專欄

evilcry2012的專欄

Java IO開銷測試比較

2015年02月08日 19:43:27 飛火流雲 閱讀數:444更多

修改了下程式碼:
  1. <pre name="code" class="java">package ringBuffer;

  2. import java.io.BufferedWriter;

  3. import java.io.FileNotFoundException;

  4. import java.io.FileOutputStream;

  5. import java.io.FileWriter;

  6. import java.io.IOException;

  7. import java.io.OutputStreamWriter;

  8. import java.io.RandomAccessFile;

  9. import java.nio.MappedByteBuffer;

  10. import java.nio.channels.FileChannel;

  11. import java.nio.channels.FileChannel.MapMode;

  12. public class PerformanceWriteTest {

  13. /**

  14. * @param args

  15. */

  16. public static void main(String[] args) {

  17. String outputFile = "F:\\test\\ioTest.txt";

  18. Long length = 0L ;

  19. Long totalTime = 0L;

  20. try {

  21. raf = new RandomAccessFile("F:\\test\\ioTest.txt", "rw");

  22. FileChannel fc = raf.getChannel();

  23. mbb = fc.map(MapMode.READ_WRITE, 0, 85*1024*1024);

  24. } catch (FileNotFoundException e) {

  25. e.printStackTrace();

  26. } catch (IOException e) {

  27. e.printStackTrace();

  28. }

  29. for (int j = 0; j < 5; j++) {

  30. // for (int j = 0; j < 5; j++) {

  31. StringBuffer sb = new StringBuffer();

  32. for (Integer i = 0; i < 1000000; i++) {

  33. // for (Integer i = 0; i < 100000; i++) {

  34. sb.append(j+i.toString() + "V");

  35. }

  36. sb.append("S");

  37. // byte[] msgs = sb.toString().getBytes();

  38. length = (long) sb.toString().length() ;

  39. long start = System.currentTimeMillis() ;

  40. appendFileTest(outputFile,sb.toString());

  41. totalTime = totalTime + (System.currentTimeMillis() - start) ;

  42. }

  43. System.out.println(" Total Data is : " + length*5/1000 + " Kbytes! ") ;

  44. System.out.println(" Total Time is : " + totalTime) ;

  45. System.out.println(" Averge Speed is :" + length*5/(totalTime*1000) + " Kbytes");

  46. }

  47. private static void appendFileTest(String outputFile, String msgs) {

  48. // append1(outputFile, msgs) ; //FileOutputStream

  49. // append2(outputFile, msgs) ; //FileWriter

  50. // append3(outputFile, msgs) ; //RandomAccessFile

  51. append4(outputFile, msgs) ; //RandomAccessFile

  52. }

  53. private static void append1(String outputFile, String msgs) {

  54. BufferedWriter out = null;

  55. try {

  56. out = new BufferedWriter(new OutputStreamWriter(

  57. new FileOutputStream(outputFile, true)));

  58. out.append(msgs) ;

  59. } catch (Exception e) {

  60. e.printStackTrace();

  61. } finally {

  62. try {

  63. out.close();

  64. } catch (IOException e) {

  65. e.printStackTrace();

  66. }

  67. }

  68. }

  69. private static void append2(String outputFile, String msgs) {

  70. try {

  71. FileWriter writer = new FileWriter(outputFile, true); // 開啟一個寫檔案器,建構函式中的第二個引數true表示以追加形式寫檔案

  72. writer.write(msgs);

  73. writer.close();

  74. } catch (IOException e) {

  75. e.printStackTrace();

  76. }

  77. }

  78. private static void append3(String outputFile, String msgs) {

  79. try {

  80. RandomAccessFile randomFile = new RandomAccessFile(outputFile, "rw"); // 開啟一個隨機訪問檔案流,按讀寫方式

  81. long fileLength = randomFile.length(); // 檔案長度,位元組數

  82. randomFile.seek(fileLength); // 將寫檔案指標移到檔案尾

  83. randomFile.writeBytes(msgs);

  84. randomFile.close();

  85. } catch (IOException e) {

  86. e.printStackTrace();

  87. }

  88. }

  89. private static void append4(String outputFile, String msgs) {

  90. try {

  91. mbb.position(pos) ;

  92. mbb.put(msgs.getBytes());

  93. pos = pos + msgs.getBytes().length ;

  94. raf.close();

  95. } catch (IOException e) {

  96. e.printStackTrace();

  97. }

  98. }

  99. static RandomAccessFile raf ;

  100. static MappedByteBuffer mbb ;

  101. static Integer pos = 0 ;

  102. }

測試時為了增加記憶體開銷,增加了 -XX:+PrintGC 選項,如果大家知道更好的測試記憶體消耗的方法,請告訴我哈。
  1. </pre><pre code_snippet_id="600488" snippet_file_name="blog_20150208_4_5933734" name="code" class="java">[GC 32704K->2872K(124992 K), 0.0015308 secs]

  2. [GC 35576K->5152K(157696K), 0.0018430 secs]

  3. [GC 70560K->9768K(157696K), 0.0032342 secs]

  4. [GC 75176K->28240K(223104K), 0.0056030 secs]

  5. [GC 159056K->28888K(223104K), 0.0009104 secs]

  6. [GC 159704K->37464K(353856K), 0.0033345 secs]

  7. [GC 299096K->42152K(354048K), 0.0022436 secs]

  8. [GC 303784K->39848K(615 744K), 0.0009303 secs]

  9. [GC 563112K->55968K(616 192K), 0.0087540 secs]

  10.  Total Data is : 39444 Kbytes! 

  11.  Total Time is : 159

  12.  Averge Speed is :248 Kbytes

  13. 2.826G

  14. [GC 32704K->2872K(124992K), 0.0136790 secs]

  15. [GC 35576K->5184K(157696K), 0.0019457 secs]

  16. [GC 70592K->9792K(157696K), 0.0031822 secs]

  17. [GC 75200K->28240K(223104K), 0.0056500 secs]

  18. [GC 152655K->43624K(223104K), 0.0051765 secs]

  19. [GC 174440K->52848K(353856K), 0.0034980 secs]

  20. [GC 314480K->55276K(354176K), 0.0013858 secs]

  21. [GC 314139K->71372K(615488K), 0.0058115 secs]

  22. [Full GC 71372K->18926K(610816K), 0.0094205 secs]

  23. [GC 542190K->43678K(610880K), 0.0038294 secs]

  24.  Total Data is : 39444 Kbytes! 

  25.  Total Time is : 153

  26.  Averge Speed is :257 Kbytesa

  27. 3.43G

  28. [GC 32704K->2872K(124992K), 0.0012018 secs]

  29. [GC 35576K->5184K(124992K), 0.0021390 secs]

  30. [GC 37888K->9800K(124992K), 0.0031359 secs]

  31. [GC 42504K->9824K(157696K), 0.0005724 secs]

  32. [GC 75232K->28208K(157696K), 0.0059463 secs]

  33. [GC 93616K->28224K(223040K), 0.0007707 secs]

  34. [GC 159040K->32852K(223360K), 0.0095536 secs]

  35. [GC 163668K->46740K(354880K), 0.0055367 secs]

  36. [GC 308372K->55892K(355008K), 0.0032216 secs]

  37. [GC 317524K->60484K(511168K), 0.0017500 secs]

  38. [GC 478468K->65092K(511808K), 0.0033206 secs]

  39.  Total Data is : 39444 Kbytes! 

  40.  Total Time is : 68

  41.  Averge Speed is :580 Kbytes

  42. 2.87G

  1. [GC 32704K->2880K(124992K), 0.0132009 secs]

  2. [GC 35584K->5192K(157696K), 0.0017013 secs]

  3. [GC 70600K->9800K(157696K), 0.0035649 secs]

  4. [GC 75208K->28232K(223104K), 0.0057867 secs]

  5. [GC 151138K->43680K(223104K), 0.0055066 secs]

  6. [GC 174496K->48256K(353344K), 0.0020662 secs]

  7. [GC 298339K->59048K(354496K), 0.0054248 secs]

  8. [GC 319528K->77512K(614592K), 0.0058665 secs]

  9. [Full GC 77512K->18933K(612224K), 0.0097774 secs]

  10. [GC 521588K->34405K(612352K), 0.0027320 secs]

  11.  Total Data is : 39444 Kbytes! 

  12.  Total Time is : 137

  13.  Averge Speed is :287 Kbytes

  14. 3.4G

這裡很奇怪,理論上Mbb的效能應該遠大於這個,可能是我的測試資料量太小的緣故.

綜上可見,RandomAccessFile 價效比最高。