11package com .baeldung .string ;
22
3+ import com .google .common .base .Splitter ;
34import org .apache .commons .lang3 .StringUtils ;
45import org .openjdk .jmh .annotations .*;
56import org .openjdk .jmh .runner .Runner ;
67import org .openjdk .jmh .runner .options .Options ;
78import org .openjdk .jmh .runner .options .OptionsBuilder ;
89
10+ import java .util .ArrayList ;
911import java .util .List ;
12+ import java .util .StringTokenizer ;
1013import java .util .concurrent .TimeUnit ;
14+ import java .util .regex .Pattern ;
1115
1216@ BenchmarkMode (Mode .SingleShotTime )
1317@ OutputTimeUnit (TimeUnit .MILLISECONDS )
14- @ Measurement (batchSize = 10000 , iterations = 10 )
15- @ Warmup (batchSize = 10000 , iterations = 10 )
16- public class StringPerformance extends StringPerformanceHints {
18+ @ Measurement (batchSize = 100000 , iterations = 10 )
19+ @ Warmup (batchSize = 100000 , iterations = 10 )
20+ @ State (Scope .Thread )
21+ public class StringPerformance {
22+
23+ protected String baeldung = "baeldung" ;
24+ protected String longString = "Hello baeldung, I am a bit longer than other Strings" ;
25+ protected String formatString = "hello %s, nice to meet you" ;
26+ protected String formatDigit = "%d" ;
27+ protected String emptyString = " " ;
28+ protected String result = "" ;
29+
30+ protected int sampleNumber = 100 ;
31+
32+ protected Pattern spacePattern = Pattern .compile (emptyString );
33+ protected Pattern longPattern = Pattern .compile (longString );
34+ protected List <String > stringSplit = new ArrayList <>();
35+ protected List <String > stringTokenizer = new ArrayList <>();
1736
1837 @ Benchmark
1938 public String benchmarkStringDynamicConcat () {
20- return dynamicConcat ();
39+ result += baeldung ;
40+ return result ;
2141 }
2242
2343 @ Benchmark
@@ -36,27 +56,30 @@ public StringBuffer benchmarkStringBuffer() {
3656
3757 @ Benchmark
3858 public String benchmarkStringConstructor () {
39- return stringConstructor ();
59+ String result = new String ("baeldung" );
60+ return result ;
4061 }
4162
4263 @ Benchmark
4364 public String benchmarkStringLiteral () {
44- return stringLiteral ();
65+ String result = "baeldung" ;
66+ return result ;
4567 }
4668
4769 @ Benchmark
4870 public String benchmarkStringFormat_s () {
49- return stringFormat_s ( );
71+ return String . format ( formatString , baeldung );
5072 }
5173
5274 @ Benchmark
5375 public String benchmarkStringConcat () {
54- return stringConcat ();
76+ result = result .concat (baeldung );
77+ return result ;
5578 }
5679
5780 @ Benchmark
5881 public String benchmarkStringIntern () {
59- return stringIntern ();
82+ return baeldung . intern ();
6083 }
6184
6285 @ Benchmark
@@ -71,85 +94,95 @@ public String benchmarkStringUtilsReplace() {
7194
7295 @ Benchmark
7396 public List <String > benchmarkGuavaSplitter () {
74- return guavaSplitter ();
97+ return Splitter .on (" " ).trimResults ()
98+ .omitEmptyStrings ()
99+ .splitToList (longString );
75100 }
76101
77102 @ Benchmark
78103 public String [] benchmarkStringSplit () {
79- return stringSplit ( );
104+ return longString . split ( emptyString );
80105 }
81106
82107 @ Benchmark
83108 public String [] benchmarkStringSplitPattern () {
84- return stringSplitPattern ( );
109+ return spacePattern . split ( longString , 0 );
85110 }
86111
87112 @ Benchmark
88113 public List benchmarkStringTokenizer () {
89- return stringTokenizer ();
114+ StringTokenizer st = new StringTokenizer (longString );
115+ while (st .hasMoreTokens ()) {
116+ stringTokenizer .add (st .nextToken ());
117+ }
118+ return stringTokenizer ;
90119 }
91120
92121 @ Benchmark
93122 public List benchmarkStringIndexOf () {
94- return stringIndexOf ();
123+ int pos = 0 , end ;
124+ while ((end = longString .indexOf (' ' , pos )) >= 0 ) {
125+ stringSplit .add (longString .substring (pos , end ));
126+ pos = end + 1 ;
127+ }
128+ return stringSplit ;
95129 }
96130
97-
98131 @ Benchmark
99132 public String benchmarkIntegerToString () {
100- return stringIntegerToString ( );
133+ return Integer . toString ( sampleNumber );
101134 }
102135
103136 @ Benchmark
104137 public String benchmarkStringValueOf () {
105- return stringValueOf ( );
138+ return String . valueOf ( sampleNumber );
106139 }
107140
108141
109142 @ Benchmark
110143 public String benchmarkStringConvertPlus () {
111- return stringConvertPlus () ;
144+ return sampleNumber + "" ;
112145 }
113146
114147 @ Benchmark
115148 public String benchmarkStringFormat_d () {
116- return stringFormat_d ( );
149+ return String . format ( formatDigit , sampleNumber );
117150 }
118151
119152 @ Benchmark
120153 public boolean benchmarkStringEquals () {
121- return stringEquals ( );
154+ return longString . equals ( baeldung );
122155 }
123156
124157
125158 @ Benchmark
126159 public boolean benchmarkStringEqualsIgnoreCase () {
127- return stringEqualsIgnoreCase ( );
160+ return longString . equalsIgnoreCase ( baeldung );
128161 }
129162
130163 @ Benchmark
131164 public boolean benchmarkStringMatches () {
132- return stringIsMatch ( );
165+ return longString . matches ( baeldung );
133166 }
134167
135168 @ Benchmark
136169 public boolean benchmarkPrecompiledMatches () {
137- return precompiledMatches ();
170+ return longPattern . matcher ( baeldung ). matches ();
138171 }
139172
140173 @ Benchmark
141174 public int benchmarkStringCompareTo () {
142- return stringCompareTo ( );
175+ return longString . compareTo ( baeldung );
143176 }
144177
145178 @ Benchmark
146179 public boolean benchmarkStringIsEmpty () {
147- return stringIsEmpty ();
180+ return longString . isEmpty ();
148181 }
149182
150183 @ Benchmark
151184 public boolean benchmarkStringLengthZero () {
152- return stringLengthZero () ;
185+ return longString . length () == 0 ;
153186 }
154187
155188 public static void main (String [] args ) throws Exception {
0 commit comments