From 5d7cc1174596ff801cda900c21599a68eda8f7b1 Mon Sep 17 00:00:00 2001 From: Mher Baghinyan Date: Sat, 22 Sep 2018 12:25:52 +0400 Subject: [PATCH] Bael 2053 (#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 --- java-strings/pom.xml | 5 + .../baeldung/string/StringPerformance.java | 163 ++++++++++++++++++ .../string/StringPerformanceHints.java | 133 ++++++++++++++ 3 files changed, 301 insertions(+) create mode 100644 java-strings/src/main/java/com/baeldung/string/StringPerformance.java create mode 100644 java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java diff --git a/java-strings/pom.xml b/java-strings/pom.xml index 29e992a2ce..b1ba49b33a 100644 --- a/java-strings/pom.xml +++ b/java-strings/pom.xml @@ -47,6 +47,11 @@ jmh-core ${jmh-core.version} + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-core.version} + com.ibm.icu icu4j diff --git a/java-strings/src/main/java/com/baeldung/string/StringPerformance.java b/java-strings/src/main/java/com/baeldung/string/StringPerformance.java new file mode 100644 index 0000000000..4873bd320c --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/StringPerformance.java @@ -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 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(); + } +} diff --git a/java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java b/java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java new file mode 100644 index 0000000000..509222136f --- /dev/null +++ b/java-strings/src/main/java/com/baeldung/string/StringPerformanceHints.java @@ -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 stringSplit = new ArrayList<>(); + protected List 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 guavaSplitter() { + return Splitter.on(" ").trimResults() + .omitEmptyStrings() + .splitToList(longString); + } + + protected String stringIntern() { + return baeldung.intern(); + } +}