第一步,我运行此代码:
public class Demo {
public static void main(String[] args) {
String x = "x";
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++)
{
x = x.concat("s");
// x+="k";
}
System.out.println(System.currentTimeMillis() - start);
}
}
出:13579。
在第二步中,我运行此代码:
public class Demo {
public static void main(String[] args) {
String x = "x";
long start = System.currentTimeMillis();
for (int i = 0; i < 100000; i++)
{
//x = x.concat("s");
x+="k";
}
System.out.println(System.currentTimeMillis() - start);
}
}
出:27328。
我有两个问题:
- 我可以说我的基准 - 正确吗?
- 为什么 (+) 和 concat() 之间的时间线差异如此之大???13.5 秒 VS 27 秒。为什么?