[Java]StringBuilderで文字列処理を高速に行うコード

Javaコード
public class Demo {

public static void main(String[] args) {
String text = “";

long beginTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
text = text + i;
long endTime = System.currentTimeMillis();
System.out.println(“cost time1:"+(endTime-beginTime));
StringBuilder sb = new StringBuilder (“");
beginTime = System.currentTimeMillis();
for(int i=0;i<10000;i++)
sb.append(String.valueOf(i));
endTime = System.currentTimeMillis();
System.out.println(“cost time2:"+(endTime-beginTime));

}

}

実行時間

cost time1:568
cost time2:4

Java

Posted by arkgame