* 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

* String performance review checked

* remove methods

* remove unnecessary class after the merge

* Delete pom.xml
This commit is contained in:
Mher Baghinyan 2018-09-27 21:02:28 +04:00 committed by Grzegorz Piwowarek
parent 0fe135bdcb
commit 2224e723fd
2 changed files with 59 additions and 159 deletions

View File

@ -1,23 +1,43 @@
package com.baeldung.string; package com.baeldung.string;
import com.google.common.base.Splitter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner; import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options; import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder; import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
@BenchmarkMode(Mode.SingleShotTime) @BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS) @OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 10000, iterations = 10) @Measurement(batchSize = 100000, iterations = 10)
@Warmup(batchSize = 10000, iterations = 10) @Warmup(batchSize = 100000, iterations = 10)
public class StringPerformance extends StringPerformanceHints { @State(Scope.Thread)
public class StringPerformance {
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<>();
@Benchmark @Benchmark
public String benchmarkStringDynamicConcat() { public String benchmarkStringDynamicConcat() {
return dynamicConcat(); result += baeldung;
return result;
} }
@Benchmark @Benchmark
@ -36,27 +56,30 @@ public class StringPerformance extends StringPerformanceHints {
@Benchmark @Benchmark
public String benchmarkStringConstructor() { public String benchmarkStringConstructor() {
return stringConstructor(); String result = new String("baeldung");
return result;
} }
@Benchmark @Benchmark
public String benchmarkStringLiteral() { public String benchmarkStringLiteral() {
return stringLiteral(); String result = "baeldung";
return result;
} }
@Benchmark @Benchmark
public String benchmarkStringFormat_s() { public String benchmarkStringFormat_s() {
return stringFormat_s(); return String.format(formatString, baeldung);
} }
@Benchmark @Benchmark
public String benchmarkStringConcat() { public String benchmarkStringConcat() {
return stringConcat(); result = result.concat(baeldung);
return result;
} }
@Benchmark @Benchmark
public String benchmarkStringIntern() { public String benchmarkStringIntern() {
return stringIntern(); return baeldung.intern();
} }
@Benchmark @Benchmark
@ -71,85 +94,95 @@ public class StringPerformance extends StringPerformanceHints {
@Benchmark @Benchmark
public List<String> benchmarkGuavaSplitter() { public List<String> benchmarkGuavaSplitter() {
return guavaSplitter(); return Splitter.on(" ").trimResults()
.omitEmptyStrings()
.splitToList(longString);
} }
@Benchmark @Benchmark
public String [] benchmarkStringSplit() { public String [] benchmarkStringSplit() {
return stringSplit(); return longString.split(emptyString);
} }
@Benchmark @Benchmark
public String [] benchmarkStringSplitPattern() { public String [] benchmarkStringSplitPattern() {
return stringSplitPattern(); return spacePattern.split(longString, 0);
} }
@Benchmark @Benchmark
public List benchmarkStringTokenizer() { public List benchmarkStringTokenizer() {
return stringTokenizer(); StringTokenizer st = new StringTokenizer(longString);
while (st.hasMoreTokens()) {
stringTokenizer.add(st.nextToken());
}
return stringTokenizer;
} }
@Benchmark @Benchmark
public List benchmarkStringIndexOf() { public List benchmarkStringIndexOf() {
return stringIndexOf(); int pos = 0, end;
while ((end = longString.indexOf(' ', pos)) >= 0) {
stringSplit.add(longString.substring(pos, end));
pos = end + 1;
}
return stringSplit;
} }
@Benchmark @Benchmark
public String benchmarkIntegerToString() { public String benchmarkIntegerToString() {
return stringIntegerToString(); return Integer.toString(sampleNumber);
} }
@Benchmark @Benchmark
public String benchmarkStringValueOf() { public String benchmarkStringValueOf() {
return stringValueOf(); return String.valueOf(sampleNumber);
} }
@Benchmark @Benchmark
public String benchmarkStringConvertPlus() { public String benchmarkStringConvertPlus() {
return stringConvertPlus(); return sampleNumber + "";
} }
@Benchmark @Benchmark
public String benchmarkStringFormat_d() { public String benchmarkStringFormat_d() {
return stringFormat_d(); return String.format(formatDigit, sampleNumber);
} }
@Benchmark @Benchmark
public boolean benchmarkStringEquals() { public boolean benchmarkStringEquals() {
return stringEquals(); return longString.equals(baeldung);
} }
@Benchmark @Benchmark
public boolean benchmarkStringEqualsIgnoreCase() { public boolean benchmarkStringEqualsIgnoreCase() {
return stringEqualsIgnoreCase(); return longString.equalsIgnoreCase(baeldung);
} }
@Benchmark @Benchmark
public boolean benchmarkStringMatches() { public boolean benchmarkStringMatches() {
return stringIsMatch(); return longString.matches(baeldung);
} }
@Benchmark @Benchmark
public boolean benchmarkPrecompiledMatches() { public boolean benchmarkPrecompiledMatches() {
return precompiledMatches(); return longPattern.matcher(baeldung).matches();
} }
@Benchmark @Benchmark
public int benchmarkStringCompareTo() { public int benchmarkStringCompareTo() {
return stringCompareTo(); return longString.compareTo(baeldung);
} }
@Benchmark @Benchmark
public boolean benchmarkStringIsEmpty() { public boolean benchmarkStringIsEmpty() {
return stringIsEmpty(); return longString.isEmpty();
} }
@Benchmark @Benchmark
public boolean benchmarkStringLengthZero() { public boolean benchmarkStringLengthZero() {
return stringLengthZero(); return longString.length() == 0;
} }
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {

View File

@ -1,133 +0,0 @@
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();
}
}