highcharts 大数据 String+,StringBuilder,String.format运行效率比较

时间:2021-11-06 12:53:54

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

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

测试代码1:

  1. <span style="white-space:pre">    </span>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. }
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
<span style="white-space:pre">	</span>String success_code = "0";

	byte splite = 0x01;

	private void method1(){
String resultMsg = "";
long time1 = System.nanoTime();
String.format("ErrorCode=%s%cErrorMsg=心跳包接收成功%c", success_code, splite, splite);
long time2 = System.nanoTime();
System.out.println("StringFormat:"+(time2-time1)+"ns"); long time3 = System.nanoTime();
resultMsg = "ErrorCode="+success_code+splite+"ErrorMsg=心跳包接收成功"+splite;
long time4 = System.nanoTime();
System.out.println("String add:"+(time4-time3)+"ns"); long time5 = System.nanoTime();
sb.append("ErrorCode=").append(success_code).append(splite).append("ErrorMsg=心跳包接收成功").append(splite);
long time6 = System.nanoTime();
System.out.println("StringBuilder add:"+(time6-time5)+"ns");
System.out.println("-------------------------------------------------");
} @Test
public void test1(){
for(int i=0; i<1000; i++){
method1();
}
}

运行结果:

  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. -------------------------------------------------
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
StringFormat:58025ns
String add:3158ns
StringBuilder add:1579ns
-------------------------------------------------
StringFormat:43026ns
String add:3948ns
StringBuilder add:1974ns
-------------------------------------------------
.....
StringFormat:46973ns
String add:1579ns
StringBuilder add:790ns
-------------------------------------------------
StringFormat:52499ns
String add:1578ns
StringBuilder add:790ns
-------------------------------------------------
StringFormat:43026ns
String add:1579ns
StringBuilder add:790ns
-------------------------------------------------

从上述结果可知,StringBuilder与String直接相加的执行效率都比String.format高, 而StringBuilder的执行效率要比String直接相加要高点。下面针对String,StringBuilder再

做一组测试。

测试代码2:

  1. <span style="white-space:pre">    </span>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. }
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
<span style="white-space:pre">	</span>public void method2(int num){
String text = "";
long beginTime = System.nanoTime();
for(int i = 0; i < num; i++){
text += i;
}
long endTime = System.nanoTime();
System.out.println("String直接相加"+num+"次耗费时间:" + (endTime - beginTime)+"ns");
StringBuilder builder = new StringBuilder("");
beginTime = System.nanoTime();
for(int i = 0; i < num; i++){
builder.append(i);
}
endTime = System.nanoTime();
System.out.println("StringBuilder相加"+num+"次耗费时间:" + (endTime - beginTime)+"ns");
System.out.println("---------------------------------------------------");
} @Test
public void test2(){
method2(10);
method2(100);
method2(10000);
method2(100000);
}

运行结果:

  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. ---------------------------------------------------
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
String直接相加10次耗费时间:19737ns
StringBuilder相加10次耗费时间:3553ns
---------------------------------------------------
String直接相加100次耗费时间:56447ns
StringBuilder相加100次耗费时间:47762ns
---------------------------------------------------
String直接相加10000次耗费时间:266082677ns
StringBuilder相加10000次耗费时间:999061ns
---------------------------------------------------
String直接相加100000次耗费时间:45212528095ns
StringBuilder相加100000次耗费时间:3040604ns
---------------------------------------------------

从测试结果可分析出,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. }
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
String success_code = "0";

	byte splite = 0x01;

	private void method1(){
String resultMsg = "";
long time1 = System.nanoTime();
resultMsg = String.format("ErrorCode=%s%cErrorMsg=心跳包接收成功%c", success_code, splite, splite);
long time2 = System.nanoTime();
System.out.println("StringFormat:"+(time2-time1)+"ns"); long time3 = System.nanoTime();
resultMsg = "ErrorCode="+success_code+splite+"ErrorMsg=心跳包接收成功"+splite;
long time4 = System.nanoTime();
System.out.println("String add:"+(time4-time3)+"ns"); long time5 = System.nanoTime();
resultMsg = sb.append("ErrorCode=").append(success_code).append(splite).append("ErrorMsg=心跳包接收成功").append(splite).toString();
long time6 = System.nanoTime();
System.out.println("StringBuilder add:"+(time6-time5)+"ns");
System.out.println("-------------------------------------------------");
}
  1. <span style="white-space:pre">    </span>@Test
  2. public void test1(){
  3. for(int i=0; i<10; i++){
  4. method1();
  5. }
  6. }
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
<span style="white-space:pre">	</span>@Test
public void test1(){
for(int i=0; i<10; i++){
method1();
}
}

运行结果:

  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. -------------------------------------------------
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
StringFormat:564859ns
String add:55657ns
StringBuilder add:3158ns
-------------------------------------------------
StringFormat:98683ns
String add:2368ns
StringBuilder add:1974ns
-------------------------------------------------
StringFormat:69867ns
String add:2369ns
StringBuilder add:1974ns
-------------------------------------------------
StringFormat:77762ns
String add:3552ns
StringBuilder add:2369ns
-------------------------------------------------
StringFormat:105788ns
String add:3948ns
StringBuilder add:2368ns
-------------------------------------------------
StringFormat:78552ns
String add:2763ns
StringBuilder add:1974ns
-------------------------------------------------
StringFormat:68683ns
String add:2368ns
StringBuilder add:1974ns
-------------------------------------------------
StringFormat:67894ns
String add:2369ns
StringBuilder add:1973ns
-------------------------------------------------
StringFormat:67499ns
String add:2369ns
StringBuilder add:1974ns
-------------------------------------------------
StringFormat:116840ns
String add:3948ns
StringBuilder add:3552ns
-------------------------------------------------

当运行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. -------------------------------------------------
highcharts 大数据 String+,StringBuilder,String.format运行效率比较
StringFormat:9079ns
String add:789ns
StringBuilder add:153550ns
-------------------------------------------------
StringFormat:18552ns
String add:789ns
StringBuilder add:141708ns
-------------------------------------------------
StringFormat:9078ns
String add:395ns
StringBuilder add:122761ns
-------------------------------------------------