Java IO开销测试比较

Java IO开销测试比较

2015年02月08日 19:43:27 飞火流云 阅读数:444更多

个人分类: Java多线程

修改了下代码:
 
  1. <pre name="code" class="java">package ringBuffer;

  2.  
  3. import java.io.BufferedWriter;

  4. import java.io.FileNotFoundException;

  5. import java.io.FileOutputStream;

  6. import java.io.FileWriter;

  7. import java.io.IOException;

  8. import java.io.OutputStreamWriter;

  9. import java.io.RandomAccessFile;

  10. import java.nio.MappedByteBuffer;

  11. import java.nio.channels.FileChannel;

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

  13.  
  14. public class PerformanceWriteTest {

  15. /**

  16. * @param args

  17. */

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

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

  21. Long length = 0L ;

  22. Long totalTime = 0L;

  23.  
  24. try {

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

  26. FileChannel fc = raf.getChannel();

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

  28. } catch (FileNotFoundException e) {

  29. e.printStackTrace();

  30. } catch (IOException e) {

  31. e.printStackTrace();

  32. }

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

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

  36. StringBuffer sb = new StringBuffer();

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

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

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

  40. }

  41. sb.append("S");

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

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

  44. long start = System.currentTimeMillis() ;

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

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

  47. }

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

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

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

  51. }

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

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

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

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

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

  58. }

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

  61. BufferedWriter out = null;

  62. try {

  63. out = new BufferedWriter(new OutputStreamWriter(

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

  65. out.append(msgs) ;

  66. } catch (Exception e) {

  67. e.printStackTrace();

  68. } finally {

  69. try {

  70. out.close();

  71. } catch (IOException e) {

  72. e.printStackTrace();

  73. }

  74. }

  75. }

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

  78. try {

  79. FileWriter writer = new FileWriter(outputFile, true); // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件

  80. writer.write(msgs);

  81. writer.close();

  82. } catch (IOException e) {

  83. e.printStackTrace();

  84. }

  85. }

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

  88. try {

  89. RandomAccessFile randomFile = new RandomAccessFile(outputFile, "rw"); // 打开一个随机访问文件流,按读写方式

  90. long fileLength = randomFile.length(); // 文件长度,字节数

  91. randomFile.seek(fileLength); // 将写文件指针移到文件尾

  92. randomFile.writeBytes(msgs);

  93. randomFile.close();

  94. } catch (IOException e) {

  95. e.printStackTrace();

  96. }

  97. }

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

  100. try {

  101. mbb.position(pos) ;

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

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

  104. raf.close();

  105. } catch (IOException e) {

  106. e.printStackTrace();

  107. }

  108. }

  109.  
  110. static RandomAccessFile raf ;

  111. static MappedByteBuffer mbb ;

  112. static Integer pos = 0 ;

  113. }

 

测试时为了增加内存开销,增加了 -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.  

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

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

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

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

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

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

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

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

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

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

  25.  Total Data is : 39444 Kbytes! 

  26.  Total Time is : 153

  27.  Averge Speed is :257 Kbytesa

  28. 3.43G

  29.  

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

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

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

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

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

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

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

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

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

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

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

  41.  Total Data is : 39444 Kbytes! 

  42.  Total Time is : 68

  43.  Averge Speed is :580 Kbytes

  44. 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 性价比最高。

猜你喜欢

转载自blog.csdn.net/evilcry2012/article/details/83578135