* 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
This commit is contained in:
Mher Baghinyan 2018-09-22 12:25:52 +04:00 committed by Grzegorz Piwowarek
parent 5a2aacc237
commit 5d7cc11745
3 changed files with 301 additions and 0 deletions

View File

@ -47,6 +47,11 @@
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>com.ibm.icu</groupId>
<artifactId>icu4j</artifactId>

View File

@ -0,0 +1,163 @@
package com.baeldung.string;
import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.List;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 10000, iterations = 10)
@Warmup(batchSize = 10000, iterations = 10)
public class StringPerformance extends StringPerformanceHints {
@Benchmark
public String benchmarkStringDynamicConcat() {
return dynamicConcat();
}
@Benchmark
public StringBuilder benchmarkStringBuilder() {
StringBuilder stringBuilder = new StringBuilder(result);
stringBuilder.append(baeldung);
return stringBuilder;
}
@Benchmark
public StringBuffer benchmarkStringBuffer() {
StringBuffer stringBuffer = new StringBuffer(result);
stringBuffer.append(baeldung);
return stringBuffer;
}
@Benchmark
public String benchmarkStringConstructor() {
return stringConstructor();
}
@Benchmark
public String benchmarkStringLiteral() {
return stringLiteral();
}
@Benchmark
public String benchmarkStringFormat_s() {
return stringFormat_s();
}
@Benchmark
public String benchmarkStringConcat() {
return stringConcat();
}
@Benchmark
public String benchmarkStringIntern() {
return stringIntern();
}
@Benchmark
public String benchmarkStringReplace() {
return longString.replace("average", " average !!!");
}
@Benchmark
public String benchmarkStringUtilsReplace() {
return StringUtils.replace(longString, "average", " average !!!");
}
@Benchmark
public List<String> benchmarkGuavaSplitter() {
return guavaSplitter();
}
@Benchmark
public String [] benchmarkStringSplit() {
return stringSplit();
}
@Benchmark
public String [] benchmarkStringSplitPattern() {
return stringSplitPattern();
}
@Benchmark
public List benchmarkStringTokenizer() {
return stringTokenizer();
}
@Benchmark
public List benchmarkStringIndexOf() {
return stringIndexOf();
}
@Benchmark
public String benchmarkIntegerToString() {
return stringIntegerToString();
}
@Benchmark
public String benchmarkStringValueOf() {
return stringValueOf();
}
@Benchmark
public String benchmarkStringConvertPlus() {
return stringConvertPlus();
}
@Benchmark
public String benchmarkStringFormat_d() {
return stringFormat_d();
}
@Benchmark
public boolean benchmarkStringEquals() {
return stringEquals();
}
@Benchmark
public boolean benchmarkStringEqualsIgnoreCase() {
return stringEqualsIgnoreCase();
}
@Benchmark
public boolean benchmarkStringMatches() {
return stringIsMatch();
}
@Benchmark
public boolean benchmarkPrecompiledMatches() {
return precompiledMatches();
}
@Benchmark
public int benchmarkStringCompareTo() {
return stringCompareTo();
}
@Benchmark
public boolean benchmarkStringIsEmpty() {
return stringIsEmpty();
}
@Benchmark
public boolean benchmarkStringLengthZero() {
return stringLengthZero();
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(StringPerformance.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,133 @@
package com.baeldung.string;
import com.google.common.base.Splitter;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Pattern;
@State(Scope.Thread)
public class StringPerformanceHints {
protected String baeldung = "baeldung";
protected String longString = "Hello baeldung, I am a bit longer than other Strings";
protected String formatString = "hello %s, nice to meet you";
protected String formatDigit = "%d";
protected String emptyString = " ";
protected String result = "";
protected int sampleNumber = 100;
protected Pattern spacePattern = Pattern.compile(emptyString);
protected Pattern longPattern = Pattern.compile(longString);
protected List<String> stringSplit = new ArrayList<>();
protected List<String> stringTokenizer = new ArrayList<>();
protected String dynamicConcat() {
result += baeldung;
return result;
}
protected String stringConstructor() {
return new String(baeldung);
}
protected String stringLiteral() {
result = baeldung;
return result;
}
protected String stringFormat_s() {
return String.format(formatString, baeldung);
}
protected String stringFormat_d() {
return String.format(formatDigit, sampleNumber);
}
protected String stringConcat() {
result = result.concat(baeldung);
return result;
}
protected List stringTokenizer() {
StringTokenizer st = new StringTokenizer(longString);
while (st.hasMoreTokens()) {
stringTokenizer.add(st.nextToken());
}
return stringTokenizer;
}
protected List stringIndexOf() {
int pos = 0, end;
while ((end = longString.indexOf(' ', pos)) >= 0) {
stringSplit.add(longString.substring(pos, end));
pos = end + 1;
}
return stringSplit;
}
protected String stringIntegerToString() {
return Integer.toString(sampleNumber);
}
protected String stringValueOf() {
return String.valueOf(sampleNumber);
}
protected String stringConvertPlus() {
return sampleNumber + "";
}
protected boolean stringEquals() {
return longString.equals(baeldung);
}
protected boolean stringEqualsIgnoreCase() {
return longString.equalsIgnoreCase(baeldung);
}
protected boolean stringIsMatch() {
return longString.matches(baeldung);
}
protected boolean precompiledMatches() {
return longPattern.matcher(baeldung).matches();
}
protected int stringCompareTo() {
return longString.compareTo(baeldung);
}
protected boolean stringIsEmpty() {
return longString.isEmpty();
}
protected boolean stringLengthZero() {
return longString.length() == 0;
}
protected String [] stringSplitPattern() {
return spacePattern.split(longString, 0);
}
protected String [] stringSplit() {
return longString.split(emptyString);
}
protected List<String> guavaSplitter() {
return Splitter.on(" ").trimResults()
.omitEmptyStrings()
.splitToList(longString);
}
protected String stringIntern() {
return baeldung.intern();
}
}