Evg*_*eev 8 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)
Run Code Online (Sandbox Code Playgroud)
注意:为了提高效率,在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);
}
Run Code Online (Sandbox Code Playgroud)
另外,将这些方法添加到String后,编译为Java编译器
String s = s1 + s2 + s3;
Run Code Online (Sandbox Code Playgroud)
将能够建立高效
String s = String.concat(s1, s2, s3);
Run Code Online (Sandbox Code Playgroud)
而不是当前的低效率
String s = (new StringBuilder(String.valueOf(s1))).append(s2).append(s3).toString();
Run Code Online (Sandbox Code Playgroud)
更新性能测试.我在我的笔记本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);
}
}
Run Code Online (Sandbox Code Playgroud)
在1,000,000次迭代中,结果如下:
version 1 = ~200 ms
version 2 = ~400 ms
Run Code Online (Sandbox Code Playgroud)
事实上,单个字符串连接表达式的性能所关联的用例并不常见.在性能受字符串连接约束的大多数情况下,它会在循环中发生,逐步构建最终产品,并且在该上下文中,mutable 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);
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
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 ==============================
Run Code Online (Sandbox Code Playgroud)