首先我运行了这段代码:
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秒。为什么?