Skip to content

Commit 1beb704

Browse files
committed
Adding String.replace to the benchmark
1 parent d6623eb commit 1beb704

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

internal-api/src/jmh/java/datadog/trace/util/StringReplacementBenchmark.java renamed to internal-api/src/jmh/java/datadog/trace/util/StringReplaceAllBenchmark.java

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package datadog.trace.util;
1+
package benchmarks.reference;
22

33
import de.thetaphi.forbiddenapis.SuppressForbidden;
44
import java.util.regex.Pattern;
@@ -9,30 +9,43 @@
99
import org.openjdk.jmh.annotations.Warmup;
1010

1111
/**
12-
* For simple replacements, Strings.replaceAll out performs String.replaceAll and
13-
* regex.Matcher.replaceAll by 3x. Strings.replaceAll also requires less allocation.
12+
* <p>For simple replacements, Strings.replaceAll is recommened.
13+
*
14+
* <p>
15+
* For simple replacements, Strings.replaceAll or String.replace out performs the regex based
16+
* methods String.replaceAll and regex.Matcher.replaceAll by 3x in terms of throughput.
17+
*
18+
* <p>String.replace and Strings.replaceAll also require less allocation.
19+
*
20+
* <p>Strings.replaceAll out performs String.replace by 1.2x in terms of throughput,
21+
* but results may vary depending on the JVM version being used.
1422
*
1523
* <p>When pattern matching is needed, compiling the regex to Pattern slightly improves overhead,
1624
* but dramatically reduces memory allocation to 1/4x of String.replaceAll. <code>
1725
* MacBook M1 with 8 threads (Java 21)
1826
*
19-
* Benchmark Mode Cnt Score Error Units
20-
* StringReplacementBenchmark.regex_replaceAll thrpt 6 13795837.811 ± 3635087.691 ops/s
21-
* StringReplacementBenchmark.regex_replaceAll:gc.alloc.rate thrpt 6 3988.955 ± 1148.316 MB/sec
27+
* <code>
28+
* MacBook M1 - 8 Threads - Java 21
29+
*
30+
* StringReplaceAllBenchmark.regex_replaceAll thrpt 6 15500559.098 ± 8640183.754 ops/s
31+
* StringReplaceAllBenchmark.regex_replaceAll:gc.alloc.rate thrpt 6 4516.464 ± 2561.063 MB/sec
32+
*
33+
* StringReplaceAllBenchmark.string_replace thrpt 6 35429131.963 ± 3203548.932 ops/s
34+
* StringReplaceAllBenchmark.string_replace:gc.alloc.rate thrpt 6 3185.108 ± 152.601 MB/sec
2235
*
23-
* StringReplacementBenchmark.string_replaceAll thrpt 6 14611046.391 ± 4865682.875 ops/s
24-
* StringReplacementBenchmark.string_replaceAll:gc.alloc.rate thrpt 6 11391.346 ± 3790.917 MB/sec
36+
* StringReplaceAllBenchmark.string_replaceAll thrpt 6 14253964.929 ± 4060225.866 ops/s
37+
* StringReplaceAllBenchmark.string_replaceAll:gc.alloc.rate thrpt 6 11114.939 ± 3129.891 MB/sec
2538
*
26-
* StringReplacementBenchmark.strings_replaceAll thrpt 6 39514695.575 ± 7169844.210 ops/s
27-
* StringReplacementBenchmark.strings_replaceAll:gc.alloc.rate thrpt 6 2777.083 ± 506.909 MB/sec
39+
* StringReplaceAllBenchmark.strings_replaceAll thrpt 6 43789250.524 ± 1910948.420 ops/s
40+
* StringReplaceAllBenchmark.strings_replaceAll:gc.alloc.rate thrpt 6 3079.973 ± 134.617 MB/sec
2841
* </code>
2942
*/
3043
@Fork(2)
3144
@Warmup(iterations = 2)
3245
@Measurement(iterations = 3)
3346
@Threads(8)
3447
@SuppressForbidden
35-
public class StringReplacementBenchmark {
48+
public class StringReplaceAllBenchmark {
3649
static final String[] INPUTS = {
3750
"foo",
3851
"baz",
@@ -65,6 +78,15 @@ static String _string_replaceAll(String input) {
6578
return input.replaceAll("foo", "*redacted*");
6679
}
6780

81+
@Benchmark
82+
public String string_replace() {
83+
return _string_replace(nextInput());
84+
}
85+
86+
static String _string_replace(String input) {
87+
return input.replace("foo", "*redacted*");
88+
}
89+
6890
static final Pattern REGEX_COMPILED = Pattern.compile("foo");
6991

7092
@Benchmark

0 commit comments

Comments
 (0)