scala 中的字符串连接是否和 Java 中的一样昂贵?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8608664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:45:38  来源:igfitidea点击:

Is string concatenation in scala as costly as it is in Java?

scalastring-concatenation

提问by bionicseraph

In Java, it's a common best practice to do string concatenation with StringBuilder due to the poor performance of appending strings using the + operator. Is the same practice recommended for Scala or has the language improved on how java performs its string concatenation?

在 Java 中,由于使用 + 运算符附加字符串的性能不佳,因此使用 StringBuilder 进行字符串连接是一种常见的最佳实践。是否为 Scala 推荐了相同的做法,或者该语言是否改进了 java 如何执行其字符串连接?

采纳答案by ruakh

Scala uses Java strings (java.lang.String), so its string concatenation is the same as Java's: the same thing is taking place in both. (The runtime is the same, after all.) There is a special StringBuilderclass in Scala, that "provides an API compatible with java.lang.StringBuilder"; see http://www.scala-lang.org/api/2.7.5/scala/StringBuilder.html.

Scala 使用 Java 字符串 ( java.lang.String),因此它的字符串连接与 Java 的相同:两者都发生了相同的事情。(毕竟运行时是一样的。)StringBuilderScala中有一个特殊的类,它“提供了一个与java.lang.StringBuilder”兼容的API ;请参阅http://www.scala-lang.org/api/2.7.5/scala/StringBuilder.html

But in terms of "best practices", I think most people would generally consider it better to write simple, clear code than maximally efficient code, except when there's an actual performance problem or a good reason to expect one. The +operator doesn't really have "poor performance", it's just that s += "foo"is equivalent to s = s + "foo"(i.e. it creates a new Stringobject), which means that, if you're doing a lot of concatenations to (what looks like) "a single string", you can avoid creating unnecessary objects — and repeatedly recopying earlier portions from one string to another — by using a StringBuilderinstead of a String. Usually the difference is not important. (Of course, "simple, clear code" is slightly contradictory: using +=is simpler, using StringBuilderis clearer. But still, the decision should usually be based on code-writing considerations rather than minor performance considerations.)

但就“最佳实践”而言,我认为大多数人通常会认为编写简单、清晰的代码比最高效率的代码更好,除非存在实际的性能问题或有充分理由期待。该+运营商并没有真正有“表现不佳”,它只是s += "foo"相当于s = s + "foo"(即,它会创建一个新的String对象),这意味着,如果你做了很多级联来(什么样子)“一个字符串",您可以避免创建不必要的对象——并通过使用 aStringBuilder而不是 a来避免重复地将前面的部分从一个字符串重新复制到另一个字符串String。通常差异并不重要。(当然,“简单、清晰的代码”有点矛盾:使用+=更简单,StringBuilder更清楚。但是,该决定通常应基于代码编写考虑而不是次要的性能考虑。)

回答by kiritsuku

Scalas String concatenation works the same way as Javas does.

Scalas 字符串连接的工作方式与 Java 相同。

val x = 5
"a"+"b"+x+"c"

is translated to

被翻译成

new StringBuilder()).append("ab").append(BoxesRunTime.boxToInteger(x)).append("c").toString()

StringBuilder is scala.collection.mutable.StringBuilder. That's the reason why the value appended to the StringBuilder is boxed by the compiler.

StringBuilder 是scala.collection.mutable.StringBuilder。这就是为什么附加到 StringBuilder 的值被编译器装箱的原因。

You can check the behavior by decompile the bytecode with javap.

您可以通过使用 javap 反编译字节码来检查行为。

回答by Display Name

I want to add: if you have a sequence of strings, then there is already a method to create a new string out of them (all items, concatenated). It's called mkString.

我想补充一点:如果您有一个字符串序列,那么已经有一种方法可以从中创建一个新字符串(所有项目,串联)。它被称为mkString

Example: (http://ideone.com/QJhkAG)

示例:(http://ideone.com/QJhkAG

val example = Seq("11111", "2222", "333", "444444")
val result = example.mkString
println(result) // prints "111112222333444444"

回答by Daniel C. Sobral

Scala uses java.lang.Stringas the type for strings, so it is subject to the same characteristics.

Scala 使用java.lang.String作为字符串的类型,因此它具有相同的特性。