String+,StringBuilder,String.format运行效率比较

转自:https://blog.csdn.net/xianymo/article/details/40583197

实现String字符串相加的方法有很多,常见的有直接相加,StringBuilder.append和String.format,这三者的运行效率是有差异的,String是final类型的,每次相加都会new一个

新的String对象,如果这种操作很多的话,很占用很大的内存。而StringBuilder.append方法是在原对象上进行操作,如果长度不够就自行扩展。

测试代码1:

  1. "white-space:pre"> String success_code = "0";
  2. byte splite = 0x01;
  3. private void method1(){
  4. String resultMsg = "";
  5. long time1 = System.nanoTime();
  6. String.format( "ErrorCode=%s%cErrorMsg=心跳包接收成功%c", success_code, splite, splite);
  7. long time2 = System.nanoTime();
  8. System.out.println( "StringFormat:"+(time2-time1)+ "ns");
  9. long time3 = System.nanoTime();
  10. resultMsg = "ErrorCode="+success_code+splite+ "ErrorMsg=心跳包接收成功"+splite;
  11. long time4 = System.nanoTime();
  12. System.out.println( "String add:"+(time4-time3)+ "ns");
  13. long time5 = System.nanoTime();
  14. sb.append( "ErrorCode=").append(success_code).append(splite).append( "ErrorMsg=心跳包接收成功").append(splite);
  15. long time6 = System.nanoTime();
  16. System.out.println( "StringBuilder add:"+(time6-time5)+ "ns");
  17. System.out.println( "-------------------------------------------------");
  18. }
  19. @Test
  20. public void test1(){
  21. for( int i= 0; i< 1000; i++){
  22. method1();
  23. }
  24. }

运行结果:

  1. StringFormat: 58025ns
  2. String add: 3158ns
  3. StringBuilder add: 1579ns
  4. -------------------------------------------------
  5. StringFormat: 43026ns
  6. String add: 3948ns
  7. StringBuilder add: 1974ns
  8. -------------------------------------------------
  9. .....
  10. StringFormat: 46973ns
  11. String add: 1579ns
  12. StringBuilder add: 790ns
  13. -------------------------------------------------
  14. StringFormat: 52499ns
  15. String add: 1578ns
  16. StringBuilder add: 790ns
  17. -------------------------------------------------
  18. StringFormat: 43026ns
  19. String add: 1579ns
  20. StringBuilder add: 790ns
  21. -------------------------------------------------
从上述结果可知,StringBuilder与String直接相加的执行效率都比String.format高, 而StringBuilder的执行效率要比String直接相加要高点。下面针对String,StringBuilder再

做一组测试。

测试代码2:

  1. "white-space:pre"> public void method2(int num){
  2. String text = "";
  3. long beginTime = System.nanoTime();
  4. for( int i = 0; i < num; i++){
  5. text += i;
  6. }
  7. long endTime = System.nanoTime();
  8. System.out.println( "String直接相加"+num+ "次耗费时间:" + (endTime - beginTime)+ "ns");
  9. StringBuilder builder = new StringBuilder( "");
  10. beginTime = System.nanoTime();
  11. for( int i = 0; i < num; i++){
  12. builder.append(i);
  13. }
  14. endTime = System.nanoTime();
  15. System.out.println( "StringBuilder相加"+num+ "次耗费时间:" + (endTime - beginTime)+ "ns");
  16. System.out.println( "---------------------------------------------------");
  17. }
  18. @Test
  19. public void test2(){
  20. method2( 10);
  21. method2( 100);
  22. method2( 10000);
  23. method2( 100000);
  24. }

运行结果:

  1. String直接相加 10次耗费时间: 19737ns
  2. StringBuilder相加 10次耗费时间: 3553ns
  3. ---------------------------------------------------
  4. String直接相加 100次耗费时间: 56447ns
  5. StringBuilder相加 100次耗费时间: 47762ns
  6. ---------------------------------------------------
  7. String直接相加 10000次耗费时间: 266082677ns
  8. StringBuilder相加 10000次耗费时间: 999061ns
  9. ---------------------------------------------------
  10. String直接相加 100000次耗费时间: 45212528095ns
  11. StringBuilder相加 100000次耗费时间: 3040604ns
  12. ---------------------------------------------------

从测试结果可分析出,StringBuilder的效率是比String高。

再来看一个测试,代码和上面的一样,只是SringBuilder加上个toString

测试代码:

  1. String success_code = "0";
  2. byte splite = 0x01;
  3. private void method1(){
  4. String resultMsg = "";
  5. long time1 = System.nanoTime();
  6. resultMsg = String.format( "ErrorCode=%s%cErrorMsg=心跳包接收成功%c", success_code, splite, splite);
  7. long time2 = System.nanoTime();
  8. System.out.println( "StringFormat:"+(time2-time1)+ "ns");
  9. long time3 = System.nanoTime();
  10. resultMsg = "ErrorCode="+success_code+splite+ "ErrorMsg=心跳包接收成功"+splite;
  11. long time4 = System.nanoTime();
  12. System.out.println( "String add:"+(time4-time3)+ "ns");
  13. long time5 = System.nanoTime();
  14. resultMsg = sb.append( "ErrorCode=").append(success_code).append(splite).append( "ErrorMsg=心跳包接收成功").append(splite).toString();
  15. long time6 = System.nanoTime();
  16. System.out.println( "StringBuilder add:"+(time6-time5)+ "ns");
  17. System.out.println( "-------------------------------------------------");
  18. }
  1. "white-space:pre"> @Test
  2. public void test1(){
  3. for( int i= 0; i< 10; i++){
  4. method1();
  5. }
  6. }
运行结果:

  1. StringFormat: 564859ns
  2. String add: 55657ns
  3. StringBuilder add: 3158ns
  4. -------------------------------------------------
  5. StringFormat: 98683ns
  6. String add: 2368ns
  7. StringBuilder add: 1974ns
  8. -------------------------------------------------
  9. StringFormat: 69867ns
  10. String add: 2369ns
  11. StringBuilder add: 1974ns
  12. -------------------------------------------------
  13. StringFormat: 77762ns
  14. String add: 3552ns
  15. StringBuilder add: 2369ns
  16. -------------------------------------------------
  17. StringFormat: 105788ns
  18. String add: 3948ns
  19. StringBuilder add: 2368ns
  20. -------------------------------------------------
  21. StringFormat: 78552ns
  22. String add: 2763ns
  23. StringBuilder add: 1974ns
  24. -------------------------------------------------
  25. StringFormat: 68683ns
  26. String add: 2368ns
  27. StringBuilder add: 1974ns
  28. -------------------------------------------------
  29. StringFormat: 67894ns
  30. String add: 2369ns
  31. StringBuilder add: 1973ns
  32. -------------------------------------------------
  33. StringFormat: 67499ns
  34. String add: 2369ns
  35. StringBuilder add: 1974ns
  36. -------------------------------------------------
  37. StringFormat: 116840ns
  38. String add: 3948ns
  39. StringBuilder add: 3552ns
  40. -------------------------------------------------
当运行10次时,均显示StringBuilder.append.toString的效率比String的直接相加高。

测试执行10000次,结果如下:

  1. StringFormat: 9079ns
  2. String add: 789ns
  3. StringBuilder add: 153550ns
  4. -------------------------------------------------
  5. StringFormat: 18552ns
  6. String add: 789ns
  7. StringBuilder add: 141708ns
  8. -------------------------------------------------
  9. StringFormat: 9078ns
  10. String add: 395ns
  11. StringBuilder add: 122761ns
  12. -------------------------------------------------
。。。。
发现当执行10000次时,出现StringBuilder的执行效率比String低了很多,原因暂未发现。

你可能感兴趣的:(String+,StringBuilder,String.format运行效率比较)