Skip to content

Instantly share code, notes, and snippets.

@doom369
Last active August 15, 2020 10:43
Show Gist options
  • Save doom369/7346b0ea98fd6493a5214ef6706a0457 to your computer and use it in GitHub Desktop.
Save doom369/7346b0ea98fd6493a5214ef6706a0457 to your computer and use it in GitHub Desktop.
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 1)
@State(Scope.Thread)
@Warmup(iterations = 5, time = 1)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Measurement(iterations = 10, time = 1)
public class StringBuilderChainingFailure {
String[] sqlColumnNames;
@Setup
public void setup() {
sqlColumnNames = new String[] {"a", "b"};
}
@Benchmark
public String noChaining() {
final StringBuilder left = new StringBuilder( "(" );
left.append( String.join( ", ", sqlColumnNames ) );
return left.append( ")" ).toString();
}
@Benchmark
public String explicitConcat() {
return "(" + String.join(", ", sqlColumnNames) + ")";
}
@Benchmark
public String explicitConcatImproved() {
String joinedColumns = String.join(", ", sqlColumnNames);
return "(" + joinedColumns + ")";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment