Hi Otávio,
this is a great proposal.
Little nit:
In cases where only 1 character is to append, I guess append(char) would be faster and at least will
save footprint in contrast to append(String).
-Ulf
Am 10.08.2014 um 23:33 schrieb Otávio Gonçalves de Santana:
*Motivation:* Make another append instead of concat String inside of append
parameter in StringBuilder class. To avoid an extra StringBuilder created
for the purpose of concatenating. So it will save memory and will faster
than concat String.
Doing a code to benchMark[1], the result is:
Benchmark Mode Samples
Mean Mean error Units
m.StringBuilderConcatBenchMark.stringBuilder thrpt 10
6317444.705 108673.584 ops/s
m.StringBuilderConcatBenchMark.stringBuilderWithConcat thrpt 10
3354554.435 68353.924 ops/s
The webrev of all code is:
https://dl.dropboxusercontent.com/u/16109193/open_jdk/string_builder_concat.zip
<https://dl.dropboxusercontent.com/u/16109193/open_jdk/string_builder_concat.zip>
[1]
@State(Scope.Thread)
@OutputTimeUnit(TimeUnit.SECONDS)
public class StringBuilderConcatBenchMark {
private static final String F = "!!!!";
private static final String E = " running in Java ";
private static final String D = " in the code ";
private static final String C = " to try impact ";
private static final String B = " with some text ";
private static final String A = "Doing a test";
@GenerateMicroBenchmark
public void stringBuilder(BlackHole bh) {
bh.consume(createBuilder(A, B, C, D, E, F));
}
@GenerateMicroBenchmark
public void stringBuilderWithConcat(BlackHole bh) {
bh.consume(createBuilderWithConcat(A, B, C, D, E, F));
}
private StringBuilder createBuilder(String... values) {
StringBuilder text = new StringBuilder();
text.append(values[0]).append(values[1])
.append(values[2]).append(values[3])
.append(values[4]).append(values[5]);
return text;
}
private StringBuilder createBuilderWithConcat(String... values) {
StringBuilder text = new StringBuilder();
text.append(values[0] + values[1])
.append(values[2] + values[3])
.append(values[4]+ values[5]);
return text;
}
}