我正在考虑向 Oracle Bug 数据库提交 RFE(增强请求),这应该会显着提高字符串连接性能。但在我这样做之前,我想听听专家们对它是否有意义的评论。
这个想法是基于这样一个事实,即现有的 String.concat(String) 在 2 个字符串上的工作速度比 StringBuilder 快两倍。问题是没有连接 3 个或更多字符串的方法。外部方法无法做到这一点,因为 String.concat 使用包私有(private)构造函数 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
最佳答案
事实是,单个字符串连接表达式的性能很重要的用例并不常见。在大多数情况下,性能受字符串连接的限制,它发生在一个循环中,逐步构建最终产品,在这种情况下,可变 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 ==============================
关于java - java.lang.String.concat 可以改进吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13778351/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我正在阅读SandiMetz的POODR,并且遇到了一个我不太了解的编码原则。这是代码:classBicycleattr_reader:size,:chain,:tire_sizedefinitialize(args={})@size=args[:size]||1@chain=args[:chain]||2@tire_size=args[:tire_size]||3post_initialize(args)endendclassMountainBike此代码将为其各自的属性输出1,2,3,4,5。我不明白的是查找方法。当一辆山地自行车被实例化时,因为它没有自己的initialize方法
我们的git存储库中目前有一个Gemfile。但是,有一个gem我只在我的环境中本地使用(我的团队不使用它)。为了使用它,我必须将它添加到我们的Gemfile中,但每次我checkout到我们的master/dev主分支时,由于与跟踪的gemfile冲突,我必须删除它。我想要的是类似Gemfile.local的东西,它将继承从Gemfile导入的gems,但也允许在那里导入新的gems以供使用只有我的机器。此文件将在.gitignore中被忽略。这可能吗? 最佳答案 设置BUNDLE_GEMFILE环境变量:BUNDLE_GEMFI
我喜欢使用Textile或Markdown为我的项目编写自述文件,但是当我生成RDoc时,自述文件被解释为RDoc并且看起来非常糟糕。有没有办法让RDoc通过RedCloth或BlueCloth而不是它自己的格式化程序运行文件?它可以配置为自动检测文件后缀的格式吗?(例如README.textile通过RedCloth运行,但README.mdown通过BlueCloth运行) 最佳答案 使用YARD直接代替RDoc将允许您包含Textile或Markdown文件,只要它们的文件后缀是合理的。我经常使用类似于以下Rake任务的东西:
我想让一个yaml对象引用另一个,如下所示:intro:"Hello,dearuser."registration:$introThanksforregistering!new_message:$introYouhaveanewmessage!上面的语法只是它如何工作的一个例子(这也是它在thiscpanmodule中的工作方式。)我正在使用标准的rubyyaml解析器。这可能吗? 最佳答案 一些yaml对象确实引用了其他对象:irb>require'yaml'#=>trueirb>str="hello"#=>"hello"ir
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www