Skip to content

SONARJAVA-5602: Modify rule S3033, benchmark performance with respect to StringBuilder size #5118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions rules/S3033/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,37 @@ if (sb.isEmpty()) {

[options="header"]
|===
| Method| Runtime| Average time| Error margin
| isEmpty| Temurin 21| 6.60 ns/op| ±0.58 ns/op
| length| Temurin 21| 6.89 ns/op| ±0.22 ns/op
| toStringEquals| Temurin 21| 10.90 ns/op| ±0.62 ns/op
| toStringIsEmpty| Temurin 21| 11.07 ns/op| ±0.94 ns/op
| Method| stringBuilderSize| Runtime| Average time| Error margin
| isEmpty| 10| Temurin 21| 6.57 ns/op| ±0.38 ns/op
| isEmpty| 100| Temurin 21| 6.68 ns/op| ±0.10 ns/op
| isEmpty| 1000| Temurin 21| 6.80 ns/op| ±0.12 ns/op
| length| 10| Temurin 21| 6.83 ns/op| ±0.12 ns/op
| length| 100| Temurin 21| 6.66 ns/op| ±0.11 ns/op
| length| 1000| Temurin 21| 6.67 ns/op| ±0.07 ns/op
| toStringEquals| 10| Temurin 21| 13.92 ns/op| ±0.18 ns/op
| toStringEquals| 100| Temurin 21| 59.09 ns/op| ±0.53 ns/op
| toStringEquals| 1000| Temurin 21| 465.79 ns/op| ±5.86 ns/op
| toStringIsEmpty| 10| Temurin 21| 13.83 ns/op| ±0.23 ns/op
| toStringIsEmpty| 100| Temurin 21| 60.06 ns/op| ±3.42 ns/op
| toStringIsEmpty| 1000| Temurin 21| 484.58 ns/op| ±4.24 ns/op
|===

*Benchmarking code*

The results were generated by running the following snippet with https://github.com/openjdk/jmh[JMH]:
The results were generated by running the following snippet with https://github.com/openjdk/jmh[JMH].

[source,java]
----
private String[] str = {"a", "b", "c", "d", "e"};
private StringBuilder sb = new StringBuilder();
@Param({"10", "100", "1000"})
int stringBuilderSize;


private StringBuilder sb;

@Setup(Level.Iteration)
public void setup() {
sb = new StringBuilder();
for (String s : str) {
sb.append(s);
}
IntStream.range(0, stringBuilderSize).forEach(i -> sb.append("word"));
}

@Benchmark
Expand Down
Loading