我有两次相同的源代码。一次在Java中,一次在Kotlin中。
算法非常简单。
首先是Java代码:
public class ConcatString implements Runnable
{
private final int o;
long start;
private long conTime;
public ConcatString(final int o)
{
this.o = o;
}
@Override
public void run()
{
start = System.currentTimeMillis();
String s = "";
for(int i = 0; i < o; i++)
{
s += i;
}
conTime = System.currentTimeMillis() - start;
}
/**
* @return the conTime
*/
public long getConTime()
{
return conTime;
}
}
这是kotlin代码:
class ConcatString(private val o: Int) : Runnable {
internal var start: Long = 0
/** @return the conTime
*/
var conTime: Long = 0
override fun run() {
start = System.currentTimeMillis()
var s = ""
for (i in 0 until o) {
s += i
}
// System.out.println(s);
conTime = System.currentTimeMillis() - start
}
}
如您所见,这很容易! 我通过计算时间比较了这两个。 Java的速度是10k,比Kotlin快,但Kotlin的速度是50k倍,它比Java快。 我不明白为什么会这样! 谁能解释我为什么?