Skip to content

SONARJAVA-5589 modify S1643: add performance benchmark table #5092

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 4 commits into from
Jun 3, 2025
Merged
Changes from 2 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
44 changes: 44 additions & 0 deletions rules/S1643/java/rule.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,50 @@ StringBuilder bld = new StringBuilder();
----


[options="header"]
|===
| Method| size| Runtime| Average time| Error margin
| plus| 100| Temurin 21| 4.19 us/op| ±0.34 us/op
| plus| 1000| Temurin 21| 377.08 us/op| ±17.36 us/op
| plus| 10000| Temurin 21| 40221.49 us/op| ±1342.76 us/op
| plus| 100000| Temurin 21| 5286.84 ms/op| ±185.79 ms/op
| stringBuilder| 100| Temurin 21| 0.97 us/op| ±0.03 us/op
| stringBuilder| 1000| Temurin 21| 10.25 us/op| ±1.64 us/op
| stringBuilder| 10000| Temurin 21| 93.27 us/op| ±16.05 us/op
| stringBuilder| 100000| Temurin 21| 1.01 ms/op| ±0.06 ms/op
|===


*Benchmarking code*

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

[source,java]
----
@Param({"100", "1000", "10000", "100000"})
int size;
private String word = "append";

@Benchmark
public String plus() {
String str = "";
for (int i = 0; i < size; i++) {
str = str + word;
}
return str;
}

@Benchmark
public String stringBuilder() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < size; i++) {
builder.append(word);
}
return builder.toString();
}
----


ifdef::env-github,rspecator-view[]

'''
Expand Down