String+,StringBuilder,String.format运行效率比较
实现String字符串相加的方法有很多,常见的有直接相加,StringBuilder.append和String.format,这三者的运行效率是有差异的,String是final类型的,每次相加都会new一个
新的String对象,如果这种操作很多的话,很占用很大的内存。而StringBuilder.append方法是在原对象上进行操作,如果长度不够就自行扩展。
测试代码1:
<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(); } }
运行结果:
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:
<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); }
运行结果:
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
测试代码:
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("-------------------------------------------------"); }
<span style="white-space:pre"> </span>@Test public void test1(){ for(int i=0; i<10; i++){ method1(); } }运行结果:
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次,结果如下:
StringFormat:9079ns String add:789ns StringBuilder add:153550ns ------------------------------------------------- StringFormat:18552ns String add:789ns StringBuilder add:141708ns ------------------------------------------------- StringFormat:9078ns String add:395ns StringBuilder add:122761ns -------------------------------------------------
。。。。发现当执行10000次时,出现StringBuilder的执行效率比String低了很多,原因暂未发现。