|
1 | | -package datadog.trace.util; |
| 1 | +package benchmarks.reference; |
2 | 2 |
|
3 | 3 | import de.thetaphi.forbiddenapis.SuppressForbidden; |
4 | 4 | import java.util.regex.Pattern; |
|
9 | 9 | import org.openjdk.jmh.annotations.Warmup; |
10 | 10 |
|
11 | 11 | /** |
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. |
14 | 22 | * |
15 | 23 | * <p>When pattern matching is needed, compiling the regex to Pattern slightly improves overhead, |
16 | 24 | * but dramatically reduces memory allocation to 1/4x of String.replaceAll. <code> |
17 | 25 | * MacBook M1 with 8 threads (Java 21) |
18 | 26 | * |
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 |
22 | 35 | * |
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 |
25 | 38 | * |
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 |
28 | 41 | * </code> |
29 | 42 | */ |
30 | 43 | @Fork(2) |
31 | 44 | @Warmup(iterations = 2) |
32 | 45 | @Measurement(iterations = 3) |
33 | 46 | @Threads(8) |
34 | 47 | @SuppressForbidden |
35 | | -public class StringReplacementBenchmark { |
| 48 | +public class StringReplaceAllBenchmark { |
36 | 49 | static final String[] INPUTS = { |
37 | 50 | "foo", |
38 | 51 | "baz", |
@@ -65,6 +78,15 @@ static String _string_replaceAll(String input) { |
65 | 78 | return input.replaceAll("foo", "*redacted*"); |
66 | 79 | } |
67 | 80 |
|
| 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 | + |
68 | 90 | static final Pattern REGEX_COMPILED = Pattern.compile("foo"); |
69 | 91 |
|
70 | 92 | @Benchmark |
|
0 commit comments