Which is the faster way to add single char to String in Java: a char(str + 'c'), or String(str + "c")?

huangapple go评论74阅读模式
英文:

Which is the faster way to add single char to String in Java: a char(str + 'c'), or String(str + "c")?

问题

我找不到关于这个主题的在线信息,虽然如果我不确定如何有效地表达这个问题,我为此道歉。

我的理论是String(str + "c")可能会更快,因为JVM在连接之前不需要先将char转换为String。Char(str + 'c')可能会更快,因为char是一种更简单的数据类型。但我不会感到惊讶,如果JDK在编译时适当地进行了优化,使这变得无关紧要。还有其他人知道这个问题的答案吗?

英文:

I couldn't find anything online about this topic, though I apologize if that's because I'm not sure how to word this question effectively.

My theory would be String(str + "c") would be faster, due to JVM not having to convert char to String first before concatenation. Char(str + 'c') could be faster due to char being a simpler datatype. But I wouldn't be surprised if JDK optimizes it properly anyway at compile time, making it irrelevant. Anyone else know the answer to this?

答案1

得分: 1

以下是您提供的代码的中文翻译部分:

你不应该对哪个更快以及它是如何工作做出任何假设这将取决于您使用的JDK以及`str`在哪里声明考虑这个在Eclipse JDK20中编译的示例

public static void main(String[] args)  {
    final String a = "hello";
    final String b = "world";

    final String c = a+"c";
    final String d = b+'c';

    final String e = args[0]+"c";
    final String f = args[1]+'c';

    System.out.format("%s - %s - %s - %s", c, d, e, f);
}

如果我在IDE中反编译`.class`文件它显示`c``d`是由编译器生成的

6  ldc <String "helloc"> [20]
8  astore_3 [c]
9  ldc <String "worldc"> [22]
11  astore 4 [d]

由于`args`在编译时未知代码包含以下等效内容

String e = new StringBuilder(args[0]).append("c").toString();
String f = new StringBuilder(args[1]).append('c').toString();

这是

13  new java.lang.StringBuilder [24]
16  dup
17  aload_0 [args]
18  iconst_0
19  aaload
20  invokestatic java.lang.String.valueOf(java.lang.Object) : java.lang.String [26]
23  invokespecial java.lang.StringBuilder(java.lang.String) [32]
26  ldc <String "c"> [35]
28  invokevirtual java.lang.StringBuilder.append(java.lang.String) : java.lang.StringBuilder [37]
31  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [41]
34  astore 5 [e]


36  new java.lang.StringBuilder [24]
39  dup
40  aload_0 [args]
41  iconst_1
42  aaload
43  invokestatic java.lang.String.valueOf(java.lang.Object) : java.lang.String [26]
46  invokespecial java.lang.StringBuilder(java.lang.String) [32]
49  bipush 99
51  invokevirtual java.lang.StringBuilder.append(char) : java.lang.StringBuilder [45]
54  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [41]
57  astore 6 [f]

不要依赖以上内容它可能会随着JDK的改进而改变也不要费心在两种连接方式之间更改代码您的性能问题可能出现在I/O数据库或网络问题中

请注意下面的注释
英文:

You shouldn't make any assumptions about which is faster, and how it works will depend on JDK you use and where str is declared. Consider this sample compiled in Eclipse JDK20:

public static void main(String[] args)  {
final String a = &quot;hello&quot;;
final String b = &quot;world&quot;;
final String c = a+&quot;c&quot;;
final String d = b+&#39;c&#39;;
final String e = args[0]+&quot;c&quot;;
final String f = args[1]+&#39;c&#39;;
System.out.format(&quot;%s - %s - %s - %s&quot;, c, d, e, f);
}

If I open the .class file in IDE decompile, it shows that c and d were generated in by compiler:

 6  ldc &lt;String &quot;helloc&quot;&gt; [20]
8  astore_3 [c]
9  ldc &lt;String &quot;worldc&quot;&gt; [22]
11  astore 4 [d]

As args isn't known at compile time, the code contains equivalent of

String e = new StringBuilder(args[0]).append(&quot;c&quot;).toString();
String f = new StringBuilder(args[1]).append(&#39;c&#39;).toString();

Which is:

13  new java.lang.StringBuilder [24]
16  dup
17  aload_0 [args]
18  iconst_0
19  aaload
20  invokestatic java.lang.String.valueOf(java.lang.Object) : java.lang.String [26]
23  invokespecial java.lang.StringBuilder(java.lang.String) [32]
26  ldc &lt;String &quot;c&quot;&gt; [35]
28  invokevirtual java.lang.StringBuilder.append(java.lang.String) : java.lang.StringBuilder [37]
31  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [41]
34  astore 5 [e]

and

36  new java.lang.StringBuilder [24]
39  dup
40  aload_0 [args]
41  iconst_1
42  aaload
43  invokestatic java.lang.String.valueOf(java.lang.Object) : java.lang.String [26]
46  invokespecial java.lang.StringBuilder(java.lang.String) [32]
49  bipush 99
51  invokevirtual java.lang.StringBuilder.append(char) : java.lang.StringBuilder [45]
54  invokevirtual java.lang.StringBuilder.toString() : java.lang.String [41]
57  astore 6 [f]

Don't rely on above, it might change across JDKs as improvements are added, and don't bother changing code between either type of concatenation. Your performance issues are likely to be in I/O or database or network issues.

Note comments below.

huangapple
  • 本文由 发表于 2023年6月6日 14:45:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定