字符串插值与串联

时间:2013-12-07 14:04:12

标签: string performance scala

字符串插值是否比连接在scala中构建字符串更高效?例如:

val myVal = "def"
val test = s"abs${myVal}ghi"

VS

val test = "abc" + myVal + "ghi"

我在问,因为我需要为toString编写class方法,其中有很多参数,因此内插字符串变得非常冗长而令我感到困惑我希望把它分成多行但是担心如果我连接它会表现不好。

1 个答案:

答案 0 :(得分:13)

我做了一个简单的例子:

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.