Skip to content

Commit 5d7cc11

Browse files
MherBaghinyanpivovarit
authored andcommitted
Bael 2053 (eugenp#5161)
* String performance class * String intern performance * String performance replace * String performance concat * String performance split * String performance convert * String performance comparison * String performance == * String performance check length * String performance * String performance matches * String performance hints * String performance %d * fixing benchmark tests * String performance final * method naming convention fix * renaming class name * remove loops
1 parent 5a2aacc commit 5d7cc11

3 files changed

Lines changed: 301 additions & 0 deletions

File tree

‎java-strings/pom.xml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>jmh-core</artifactId>
4848
<version>${jmh-core.version}</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>org.openjdk.jmh</groupId>
52+
<artifactId>jmh-generator-annprocess</artifactId>
53+
<version>${jmh-core.version}</version>
54+
</dependency>
5055
<dependency>
5156
<groupId>com.ibm.icu</groupId>
5257
<artifactId>icu4j</artifactId>
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package com.baeldung.string;
2+
3+
import org.apache.commons.lang3.StringUtils;
4+
import org.openjdk.jmh.annotations.*;
5+
import org.openjdk.jmh.runner.Runner;
6+
import org.openjdk.jmh.runner.options.Options;
7+
import org.openjdk.jmh.runner.options.OptionsBuilder;
8+
9+
import java.util.List;
10+
import java.util.concurrent.TimeUnit;
11+
12+
@BenchmarkMode(Mode.SingleShotTime)
13+
@OutputTimeUnit(TimeUnit.MILLISECONDS)
14+
@Measurement(batchSize = 10000, iterations = 10)
15+
@Warmup(batchSize = 10000, iterations = 10)
16+
public class StringPerformance extends StringPerformanceHints {
17+
18+
@Benchmark
19+
public String benchmarkStringDynamicConcat() {
20+
return dynamicConcat();
21+
}
22+
23+
@Benchmark
24+
public StringBuilder benchmarkStringBuilder() {
25+
StringBuilder stringBuilder = new StringBuilder(result);
26+
stringBuilder.append(baeldung);
27+
return stringBuilder;
28+
}
29+
30+
@Benchmark
31+
public StringBuffer benchmarkStringBuffer() {
32+
StringBuffer stringBuffer = new StringBuffer(result);
33+
stringBuffer.append(baeldung);
34+
return stringBuffer;
35+
}
36+
37+
@Benchmark
38+
public String benchmarkStringConstructor() {
39+
return stringConstructor();
40+
}
41+
42+
@Benchmark
43+
public String benchmarkStringLiteral() {
44+
return stringLiteral();
45+
}
46+
47+
@Benchmark
48+
public String benchmarkStringFormat_s() {
49+
return stringFormat_s();
50+
}
51+
52+
@Benchmark
53+
public String benchmarkStringConcat() {
54+
return stringConcat();
55+
}
56+
57+
@Benchmark
58+
public String benchmarkStringIntern() {
59+
return stringIntern();
60+
}
61+
62+
@Benchmark
63+
public String benchmarkStringReplace() {
64+
return longString.replace("average", " average !!!");
65+
}
66+
67+
@Benchmark
68+
public String benchmarkStringUtilsReplace() {
69+
return StringUtils.replace(longString, "average", " average !!!");
70+
}
71+
72+
@Benchmark
73+
public List<String> benchmarkGuavaSplitter() {
74+
return guavaSplitter();
75+
}
76+
77+
@Benchmark
78+
public String [] benchmarkStringSplit() {
79+
return stringSplit();
80+
}
81+
82+
@Benchmark
83+
public String [] benchmarkStringSplitPattern() {
84+
return stringSplitPattern();
85+
}
86+
87+
@Benchmark
88+
public List benchmarkStringTokenizer() {
89+
return stringTokenizer();
90+
}
91+
92+
@Benchmark
93+
public List benchmarkStringIndexOf() {
94+
return stringIndexOf();
95+
}
96+
97+
98+
@Benchmark
99+
public String benchmarkIntegerToString() {
100+
return stringIntegerToString();
101+
}
102+
103+
@Benchmark
104+
public String benchmarkStringValueOf() {
105+
return stringValueOf();
106+
}
107+
108+
109+
@Benchmark
110+
public String benchmarkStringConvertPlus() {
111+
return stringConvertPlus();
112+
}
113+
114+
@Benchmark
115+
public String benchmarkStringFormat_d() {
116+
return stringFormat_d();
117+
}
118+
119+
@Benchmark
120+
public boolean benchmarkStringEquals() {
121+
return stringEquals();
122+
}
123+
124+
125+
@Benchmark
126+
public boolean benchmarkStringEqualsIgnoreCase() {
127+
return stringEqualsIgnoreCase();
128+
}
129+
130+
@Benchmark
131+
public boolean benchmarkStringMatches() {
132+
return stringIsMatch();
133+
}
134+
135+
@Benchmark
136+
public boolean benchmarkPrecompiledMatches() {
137+
return precompiledMatches();
138+
}
139+
140+
@Benchmark
141+
public int benchmarkStringCompareTo() {
142+
return stringCompareTo();
143+
}
144+
145+
@Benchmark
146+
public boolean benchmarkStringIsEmpty() {
147+
return stringIsEmpty();
148+
}
149+
150+
@Benchmark
151+
public boolean benchmarkStringLengthZero() {
152+
return stringLengthZero();
153+
}
154+
155+
public static void main(String[] args) throws Exception {
156+
Options options = new OptionsBuilder()
157+
.include(StringPerformance.class.getSimpleName()).threads(1)
158+
.forks(1).shouldFailOnError(true)
159+
.shouldDoGC(true)
160+
.jvmArgs("-server").build();
161+
new Runner(options).run();
162+
}
163+
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.baeldung.string;
2+
3+
import com.google.common.base.Splitter;
4+
import org.openjdk.jmh.annotations.Scope;
5+
import org.openjdk.jmh.annotations.State;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
import java.util.StringTokenizer;
10+
import java.util.regex.Pattern;
11+
12+
@State(Scope.Thread)
13+
public class StringPerformanceHints {
14+
15+
protected String baeldung = "baeldung";
16+
protected String longString = "Hello baeldung, I am a bit longer than other Strings";
17+
protected String formatString = "hello %s, nice to meet you";
18+
protected String formatDigit = "%d";
19+
protected String emptyString = " ";
20+
protected String result = "";
21+
22+
protected int sampleNumber = 100;
23+
24+
protected Pattern spacePattern = Pattern.compile(emptyString);
25+
protected Pattern longPattern = Pattern.compile(longString);
26+
protected List<String> stringSplit = new ArrayList<>();
27+
protected List<String> stringTokenizer = new ArrayList<>();
28+
29+
protected String dynamicConcat() {
30+
result += baeldung;
31+
return result;
32+
}
33+
34+
protected String stringConstructor() {
35+
return new String(baeldung);
36+
}
37+
38+
protected String stringLiteral() {
39+
result = baeldung;
40+
return result;
41+
}
42+
43+
protected String stringFormat_s() {
44+
return String.format(formatString, baeldung);
45+
}
46+
47+
protected String stringFormat_d() {
48+
return String.format(formatDigit, sampleNumber);
49+
}
50+
51+
protected String stringConcat() {
52+
result = result.concat(baeldung);
53+
return result;
54+
}
55+
56+
protected List stringTokenizer() {
57+
StringTokenizer st = new StringTokenizer(longString);
58+
while (st.hasMoreTokens()) {
59+
stringTokenizer.add(st.nextToken());
60+
}
61+
return stringTokenizer;
62+
}
63+
64+
protected List stringIndexOf() {
65+
int pos = 0, end;
66+
while ((end = longString.indexOf(' ', pos)) >= 0) {
67+
stringSplit.add(longString.substring(pos, end));
68+
pos = end + 1;
69+
}
70+
return stringSplit;
71+
}
72+
73+
protected String stringIntegerToString() {
74+
return Integer.toString(sampleNumber);
75+
}
76+
77+
protected String stringValueOf() {
78+
return String.valueOf(sampleNumber);
79+
}
80+
81+
82+
protected String stringConvertPlus() {
83+
return sampleNumber + "";
84+
}
85+
86+
87+
protected boolean stringEquals() {
88+
return longString.equals(baeldung);
89+
}
90+
91+
92+
protected boolean stringEqualsIgnoreCase() {
93+
return longString.equalsIgnoreCase(baeldung);
94+
}
95+
96+
protected boolean stringIsMatch() {
97+
return longString.matches(baeldung);
98+
}
99+
100+
protected boolean precompiledMatches() {
101+
return longPattern.matcher(baeldung).matches();
102+
}
103+
104+
protected int stringCompareTo() {
105+
return longString.compareTo(baeldung);
106+
}
107+
108+
protected boolean stringIsEmpty() {
109+
return longString.isEmpty();
110+
}
111+
112+
protected boolean stringLengthZero() {
113+
return longString.length() == 0;
114+
}
115+
116+
protected String [] stringSplitPattern() {
117+
return spacePattern.split(longString, 0);
118+
}
119+
120+
protected String [] stringSplit() {
121+
return longString.split(emptyString);
122+
}
123+
124+
protected List<String> guavaSplitter() {
125+
return Splitter.on(" ").trimResults()
126+
.omitEmptyStrings()
127+
.splitToList(longString);
128+
}
129+
130+
protected String stringIntern() {
131+
return baeldung.intern();
132+
}
133+
}

0 commit comments

Comments
 (0)