String和stringbuild性能比较

package com.jdk.api.test;

public class Strings {


public static void main(String[] args)
{

String a1 = null;
// 事实证明证明使用stringbuilder 比使用string 连加操作快很多
StringBuilder sb = new StringBuilder();

long start = System.currentTimeMillis();
for(int i = 0; i< 100000;i++){
   a1=a1+"al";
}

long end = System.currentTimeMillis();
System.out.println("--:"+(end-start));
start = System.currentTimeMillis();
for(int i = 0; i< 100000;i++){
sb = sb.append("sb");
}
end = System.currentTimeMillis();
System.out.println("--:"+(end-start));

}
/*
* --:39394
--:13
*/
}

猜你喜欢

转载自lixuan74.iteye.com/blog/1709163
今日推荐