很難相信,你可以測量任何有意義的東西以這種方式,所以這是一個有點像臆斷多少天使可以在別針的頭上跳舞。
有用於檢查選擇你的代碼編譯爲:
[email protected]:~/tmp$ skalac -Xprint:typer angeldance.scala
[[syntax trees at end of typer]] // angeldance.scala
package angeldance {
object Main extends scala.AnyRef {
def <init>(): angeldance.Main.type = {
Main.super.<init>();
()
};
def main(args: Array[String]): Unit = {
{
val start: Long = java.this.lang.System.currentTimeMillis();
scala.this.Predef.println("took ".+(java.this.lang.System.currentTimeMillis().-(start)).+(" ms"))
};
{
val start: Long = java.this.lang.System.currentTimeMillis();
val took: Long = java.this.lang.System.currentTimeMillis().-(start);
scala.this.Predef.println(scala.StringContext.apply("took ", " ms").s(took))
}
}
}
}
在第二種情況下,StringContext.s
使用java.lang.StringBuilder
,而在第一種情況下表達式使用scala/collection/mutable/StringBuilder
。這可能會導致課堂上的費用。你必須非常好奇,試試-XX:+PrintClassLoading
。將其調用爲scala -J-XX:+PrintClassLoading
。
scala> val i = 5L
i: Long = 5
scala> "hello, " + i
res0: String = hello, 5
scala> :javap -
Size 1007 bytes
MD5 checksum bbccca3ecafe9287f07df81ea123676e
Compiled from "<console>"
[snip]
8: aload_0
9: new #23 // class scala/collection/mutable/StringBuilder
12: dup
13: invokespecial #24 // Method scala/collection/mutable/StringBuilder."<init>":()V
16: ldc #26 // String hello,
18: invokevirtual #30 // Method scala/collection/mutable/StringBuilder.append:(Ljava/lang/Object;)Lscala/collection/mutable/StringBuilder;
21: getstatic #35 // Field .MODULE$:L;
24: invokevirtual #39 // Method .i:()J
27: invokestatic #45 // Method scala/runtime/BoxesRunTime.boxToLong:(J)Ljava/lang/Long;
30: invokevirtual #30 // Method scala/collection/mutable/StringBuilder.append:(Ljava/lang/Object;)Lscala/collection/mutable/StringBuilder;
33: invokevirtual #48 // Method scala/collection/mutable/StringBuilder.toString:()Ljava/lang/String;
附加的類加載看起來像一個正確的答案,謝謝。 – Tvaroh