Ответ 1
Я сделал простой пример:
class Employee(name: String, age: Int) {
private var s = 0.0
def salary = s
def salary_=(s: Double) = this.s = s
def toStringConcat(): String = {
"Name: " + name + ", age: " + age + ", salary: " + salary
}
def toStringInterpol(): String = {
s"Name: $name, age: $age, salary: $salary"
}
}
object Program {
val empl = new Employee("John", 30)
empl.salary = 10.50
val times = 10000000;
def main(args: Array[String]): Unit = {
// warming-up
val resultConcat = empl.toStringConcat
val resultInterpol = empl.toStringInterpol
println("Concat -> " + resultConcat)
println("Interpol -> " + resultInterpol)
val secondsConcat0 = run(empl.toStringConcat)
val secondsInterpol0 = run(empl.toStringInterpol)
val secondsConcat1 = run(empl.toStringConcat)
val secondsInterpol1 = run(empl.toStringInterpol)
println("Concat-0: " + secondsConcat0 + "s")
println("Concat-1: " + secondsConcat1 + "s")
println("Interpol-0: " + secondsInterpol0 + "s")
println("Interpol-1: " + secondsInterpol1 + "s")
}
def run(call: () => String): Double = {
val time0 = System.nanoTime()
var result = ""
for (i <- 0 until times) {
result = call()
}
val time1 = System.nanoTime()
val elapsedTime = time1 - time0;
val seconds = elapsedTime / 1000000000.0;
seconds
}
}
Результаты были следующими:
Concat -> Name: John, age: 30, salary: 10.5
Interpol -> Name: John, age: 30, salary: 10.5
Concat-0: 2.831298161s
Concat-1: 2.725815448s
Interpol-0: 3.846891864s
Interpol-1: 3.753401004s
Случай интерполяции был медленнее. Причина может быть найдена в сгенерированном коде:
public String toStringConcat()
{
return new StringBuilder().append("Name: ").append(this.name).append(", age: ").append(BoxesRunTime.boxToInteger(this.age)).append(", salary: ").append(BoxesRunTime.boxToDouble(salary())).toString();
}
public String toStringInterpol()
{
return new StringContext(Predef..MODULE$.wrapRefArray((Object[])new String[] { "Name: ", ", age: ", ", salary: ", "" })).s(Predef..MODULE$.genericWrapArray(new Object[] { this.name, BoxesRunTime.boxToInteger(this.age), BoxesRunTime.boxToDouble(salary()) }));
}
Я использовал Scala 2.10.3 и IntelliJ IDEA 13.