Bael 2053 (#5331)
* 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:
parent
0fe135bdcb
commit
2224e723fd
|
@ -1,23 +1,43 @@
|
|||
package com.baeldung.string;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
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.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.StringTokenizer;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@BenchmarkMode(Mode.SingleShotTime)
|
||||
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||
@Measurement(batchSize = 10000, iterations = 10)
|
||||
@Warmup(batchSize = 10000, iterations = 10)
|
||||
public class StringPerformance extends StringPerformanceHints {
|
||||
@Measurement(batchSize = 100000, iterations = 10)
|
||||
@Warmup(batchSize = 100000, iterations = 10)
|
||||
@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
|
||||
public String benchmarkStringDynamicConcat() {
|
||||
return dynamicConcat();
|
||||
result += baeldung;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
|
@ -36,27 +56,30 @@ public class StringPerformance extends StringPerformanceHints {
|
|||
|
||||
@Benchmark
|
||||
public String benchmarkStringConstructor() {
|
||||
return stringConstructor();
|
||||
String result = new String("baeldung");
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringLiteral() {
|
||||
return stringLiteral();
|
||||
String result = "baeldung";
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringFormat_s() {
|
||||
return stringFormat_s();
|
||||
return String.format(formatString, baeldung);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringConcat() {
|
||||
return stringConcat();
|
||||
result = result.concat(baeldung);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringIntern() {
|
||||
return stringIntern();
|
||||
return baeldung.intern();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
|
@ -71,85 +94,95 @@ public class StringPerformance extends StringPerformanceHints {
|
|||
|
||||
@Benchmark
|
||||
public List<String> benchmarkGuavaSplitter() {
|
||||
return guavaSplitter();
|
||||
return Splitter.on(" ").trimResults()
|
||||
.omitEmptyStrings()
|
||||
.splitToList(longString);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String [] benchmarkStringSplit() {
|
||||
return stringSplit();
|
||||
return longString.split(emptyString);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String [] benchmarkStringSplitPattern() {
|
||||
return stringSplitPattern();
|
||||
return spacePattern.split(longString, 0);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public List benchmarkStringTokenizer() {
|
||||
return stringTokenizer();
|
||||
StringTokenizer st = new StringTokenizer(longString);
|
||||
while (st.hasMoreTokens()) {
|
||||
stringTokenizer.add(st.nextToken());
|
||||
}
|
||||
return stringTokenizer;
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
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
|
||||
public String benchmarkIntegerToString() {
|
||||
return stringIntegerToString();
|
||||
return Integer.toString(sampleNumber);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringValueOf() {
|
||||
return stringValueOf();
|
||||
return String.valueOf(sampleNumber);
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringConvertPlus() {
|
||||
return stringConvertPlus();
|
||||
return sampleNumber + "";
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public String benchmarkStringFormat_d() {
|
||||
return stringFormat_d();
|
||||
return String.format(formatDigit, sampleNumber);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean benchmarkStringEquals() {
|
||||
return stringEquals();
|
||||
return longString.equals(baeldung);
|
||||
}
|
||||
|
||||
|
||||
@Benchmark
|
||||
public boolean benchmarkStringEqualsIgnoreCase() {
|
||||
return stringEqualsIgnoreCase();
|
||||
return longString.equalsIgnoreCase(baeldung);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean benchmarkStringMatches() {
|
||||
return stringIsMatch();
|
||||
return longString.matches(baeldung);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean benchmarkPrecompiledMatches() {
|
||||
return precompiledMatches();
|
||||
return longPattern.matcher(baeldung).matches();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public int benchmarkStringCompareTo() {
|
||||
return stringCompareTo();
|
||||
return longString.compareTo(baeldung);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean benchmarkStringIsEmpty() {
|
||||
return stringIsEmpty();
|
||||
return longString.isEmpty();
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public boolean benchmarkStringLengthZero() {
|
||||
return stringLengthZero();
|
||||
return longString.length() == 0;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue