Java 8:字符串连接操作对性能有显着影响

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24053499/
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-08-14 09:54:43  来源:igfitidea点击:

Java 8 : String join operation has significant performance impact

javajava-8

提问by dgm

I was going through the newly added existing features introduced in Java-8. One simple feature newly added to String class is quiet appealing for me – that is String Join method.

我正在浏览 Java-8 中引入的新添加的现有功能。新添加到 String 类的一个简单功能对我来说很有吸引力——那就是String Join 方法

Example:

例子:

String.join(" ", "AZY","BAX"); // returns AZY BAX

For curiosity, I have checked the performance (execution time) of this feature by writing a simple java code

出于好奇,我通过编写一个简单的 java 代码检查了此功能的性能(执行时间)

public static void main(String[] args) {
    long start = System.nanoTime();
    String abc= String.join(" ,"AZY","BAX" … // joining 1000 words of size 3 char;
    long diff = System.nanoTime() - start;
    System.out.println(" Java 8 String Join " + diff);

     start = System.nanoTime();
    abc= "AZY"+"BAX"+"CBA"+ … // adding 1000 word of size 3 char;
    diff = System.nanoTime() - start;
    System.out.println(" Tranditional " + diff);

    start = System.nanoTime();
    new StringBuilder().append("AZY").append("BAX").appe… // appending 1000 word of size 3 char;
    diff = System.nanoTime() - start;
    System.out.println(" String Builder Append " + diff);

}

The result is not so exciting for me (time in neno sec)

结果对我来说并不那么令人兴奋(时间以尼诺秒为单位)

Java 8 String Join     1340114
Tranditional             59785
String Builder Append   102807

The complexity is of o(n) – in-fact it is (n * Size of individual element length)

复杂度为 o(n) – 事实上它是 (n * 单个元素长度的大小)

Other performance measures (memory etc) I have not measured.

我还没有测量过的其他性能指标(内存等)。

My questions are:

我的问题是:

  1. Is there anything wrong in my measurement (most of the time I believe on the jdk guys)
  2. What is the intent of adding “join” API to String class
  3. Is there any performance analysis for Java 8 is available
  1. 我的测量有什么问题吗(大部分时间我相信 jdk 家伙)
  2. 向 String 类添加“join”API 的意图是什么
  3. 是否有针对 Java 8 的性能分析可用

采纳答案by Boris the Spider

First things first. This is not how you microbench Java

先说第一件事。这不是你对 Java 进行微基准测试的方式

Read How do I write a correct micro-benchmark in Java?first. Your numbers are completely irrelevant, so lets ignore them.

阅读如何用 Java 编写正确的微基准测试?第一的。你的数字是完全无关的,所以让我们忽略它们。

Looking at the second example:

看第二个例子:

abc= "AZY"+"BAX"+"CBA"+...

These look like compile time constants to me. This Stringwould be concatenated at compile time and there would be nothing to benchmark. This is a useless comparison as the whole point of the StringBuilderor String.joinis to concatenate Strings that are not compile time constant.

这些对我来说就像编译时常量。这String将在编译时连接起来,并且没有什么可以进行基准测试。这是一个无用的比较,因为StringBuilderor 的重点String.join是连接String不是编译时常量的 s。

Moving onto comparing the StringBuilderand String.join. Looking at the source code:

继续比较StringBuilderString.join。查看源代码:

public static String join(CharSequence delimiter, CharSequence... elements) {
    Objects.requireNonNull(delimiter);
    Objects.requireNonNull(elements);
    // Number of elements not likely worth Arrays.stream overhead.
    StringJoiner joiner = new StringJoiner(delimiter);
    for (CharSequence cs: elements) {
        joiner.add(cs);
    }
    return joiner.toString();
}

This uses a StringJoiner. A StringJoinersimply uses a StringBuilderunder the hood, so the two are equivalent.

这使用一个StringJoiner. AStringJoiner只是StringBuilder在引擎盖下使用了 a ,所以两者是等价的。

It is often much more informative to look at the codethan to try and benchmark performance. Even if you do benchmark correctly.

查看代码通常比尝试和基准性能提供更多信息。即使您正确执行基准测试。

It's also worth noting that your first method, with join, joins the 1000 Strings on " " (space). Whereas your StringBuildermethod simply appends them together. These two are not the same.

还值得注意的是,您的第一种方法 withjoin将 1000 Strings 加入“”(空格)。而您的StringBuilder方法只是将它们附加在一起。这两个不一样。

The point of the String.joinmethod is that you can do:

String.join方法的要点是您可以执行以下操作:

String.join(", ", "a", "b", "c") // result is "a, b, c"

With a StringBuilderyou would have to add a lot more code.

使用 aStringBuilder您将不得不添加更多代码。