可以改进java.lang.String.concat吗?

时间:2012-12-08 14:23:58

标签: java string string-concatenation

我正在考虑向Oracle Bug数据库提交一份RFE(增强请求),该数据库可以显着提高字符串连接性能。但在我这样做之前,我想听听专家对这是否有意义的评论。

这个想法是基于以下事实:现有的String.concat(String)在2个字符串上的工作速度比StringBuilder快两倍。问题是没有方法来连接3个或更多字符串。外部方法不能这样做,因为String.concat使用包私有构造函数String(int offset, int count, char[] value),它不复制char数组但直接使用它。这确保了高String.concat性能。在同一个包中,StringBuilder仍然无法使用此构造函数,因为String的char数组将被公开以进行修改。

我建议将以下方法添加到String

public static String concat(String s1, String s2) 
public static String concat(String s1, String s2, String s3) 
public static String concat(String s1, String s2, String s3, String s4) 
public static String concat(String s1, String s2, String s3, String s4, String s5) 
public static String concat(String s1, String... array) 

注意:为了提高效率,在EnumSet.of中使用了这种重载。

这是其中一种方法的实现,其他方法的工作方式相同

public final class String {
    private final char value[];
    private final int count;
    private final int offset;

    String(int offset, int count, char value[]) {
        this.value = value;
        this.offset = offset;
        this.count = count;
    }

    public static String concat(String s1, String s2, String s3) {
        char buf[] = new char[s1.count + s2.count + s3.count];
        System.arraycopy(s1.value, s1.offset, buf, 0, s1.count);
        System.arraycopy(s2.value, s2.offset, buf, s1.count, s2.count);
        System.arraycopy(s3.value, s3.offset, buf, s1.count + s2.count, s3.count);
        return new String(0, buf.length, buf);
    }

此外,将这些方法添加到String,Java编译器

之后
String s = s1 + s2 + s3;

将能够建立高效的

String s = String.concat(s1, s2, s3); 

而非当前的低效率

String s = (new StringBuilder(String.valueOf(s1))).append(s2).append(s3).toString();

更新性能测试。我在我的笔记本Intel Celeron 925上运行它,连接3个字符串,我的String2类完全模仿它在真正的java.lang.String中的表现。选择字符串长度,以便将StringBuilder置于最不利的条件下,即需要在每个附加上扩展其内部缓冲区容量时,而concat始终只创建char []一次。

public class String2 {
    private final char value[];
    private final int count;
    private final int offset;

    String2(String s) {
        value = s.toCharArray();
        offset = 0;
        count = value.length;
    }

    String2(int offset, int count, char value[]) {
        this.value = value;
        this.offset = offset;
        this.count = count;
    }

    public static String2 concat(String2 s1, String2 s2, String2 s3) {
        char buf[] = new char[s1.count + s2.count + s3.count];
        System.arraycopy(s1.value, s1.offset, buf, 0, s1.count);
        System.arraycopy(s2.value, s2.offset, buf, s1.count, s2.count);
        System.arraycopy(s3.value, s3.offset, buf, s1.count + s2.count, s3.count);
        return new String2(0, buf.length, buf);
    }

    public static void main(String[] args) {
        String s1 = "1";
        String s2 = "11111111111111111";
        String s3 = "11111111111111111111111111111111111111111";
        String2 s21 = new String2(s1);
        String2 s22 = new String2(s2);
        String2 s23 = new String2(s3);
        long t0 = System.currentTimeMillis();
        for (int i = 0; i < 1000000; i++) {
            String2 s = String2.concat(s21, s22, s23);
//          String s = new StringBuilder(s1).append(s2).append(s3).toString();
        }
        System.out.println(System.currentTimeMillis() - t0);
    }
}

在1,000,000次迭代中,结果如下:

version 1 = ~200 ms
version 2 = ~400 ms

3 个答案:

答案 0 :(得分:7)

事实上,单个字符串连接表达式的性能所关联的用例并不常见。在性能受字符串连接限制的大多数情况下,它会在循环中发生,逐步构建最终产品,并且在该上下文中,可变StringBuilder是明显的赢家。这就是为什么我没有看到通过干预基础String课程来优化少数群体关注的提案的视角。但无论如何,就性能而言,您的方法确实具有重要优势:

import com.google.caliper.Runner;
import com.google.caliper.SimpleBenchmark;

public class Performance extends SimpleBenchmark
{
  final Random rnd = new Random();
  final String as1 = "aoeuaoeuaoeu", as2 = "snthsnthnsth", as3 = "3453409345";
  final char[] c1 = as1.toCharArray(), c2 = as2.toCharArray(), c3 = as3.toCharArray();

  public static char[] concat(char[] s1, char[] s2, char[] s3) {
    char buf[] = new char[s1.length + s2.length + s3.length];
    System.arraycopy(s1, 0, buf, 0, s1.length);
    System.arraycopy(s2, 0, buf, s1.length, s2.length);
    System.arraycopy(s3, 0, buf, s1.length + s2.length, s3.length);
    return buf;
  }

  public static String build(String s1, String s2, String s3) {
    final StringBuilder b = new StringBuilder(s1.length() + s2.length() + s3.length());
    b.append(s1).append(s2).append(s3);
    return b.toString();
  }

  public static String plus(String s1, String s2, String s3) {
    return s1 + s2 + s3;
  }

  public int timeConcat(int reps) {
    int tot = rnd.nextInt();
    for (int i = 0; i < reps; i++) tot += concat(c1, c2, c3).length;
    return tot;
  }

  public int timeBuild(int reps) {
    int tot = rnd.nextInt();
    for (int i = 0; i < reps; i++) tot += build(as1, as2, as3).length();
    return tot;
  }

  public int timePlus(int reps) {
    int tot = rnd.nextInt();
    for (int i = 0; i < reps; i++) tot += plus(as1, as2, as3).length();
    return tot;
  }

  public static void main(String... args) {
    Runner.main(Performance.class, args);
  }
}

结果:

 0% Scenario{vm=java, trial=0, benchmark=Concat} 65.81 ns; σ=2.56 ns @ 10 trials
33% Scenario{vm=java, trial=0, benchmark=Build} 102.94 ns; σ=2.27 ns @ 10 trials
67% Scenario{vm=java, trial=0, benchmark=Plus} 160.14 ns; σ=2.94 ns @ 10 trials

benchmark    ns linear runtime
   Concat  65.8 ============
    Build 102.9 ===================
     Plus 160.1 ==============================

答案 1 :(得分:4)

如果您希望他们认真对待您,您需要做好全面实施,测试并对您提议的更改进行全面基准测试的艰苦工作。完整的实现将包括对Java编译器的更改以发出字节码以使用您的方法。

编写结果,然后将代码更改作为补丁提交给OpenJDK 7或8。

我的印象是Java开发人员没有资源来尝试像这样的优化的推测性想法。没有基准测试结果和代码补丁的RFE不太可能受到关注......

答案 2 :(得分:1)

问他们总是好的,别担心。

我不会有这么多重载版本。在EnumSet中,保存可能很重要;在String中不太可能。

实际上我认为允许任意数量的args的静态方法更好

    public static String join(String... strings)

因为args的数量在编译时可能是未知的。

相关问题