From 7904c3ee04742f0637200e2dcf5df159a3f7c666 Mon Sep 17 00:00:00 2001 From: iaforek Date: Wed, 6 Sep 2017 07:31:52 +0100 Subject: [PATCH] BAEL-1065 Simple performance check StringBuffer vs StringBuilder. (#2512) * Code for Dependency Injection Article. * Added Java based configuration. Downloaded formatter.xml and reformatted all changed files. Manually changed tab into 4 spaces in XML configuration files. * BAEL-434 - Spring Roo project files generated by Spring Roo. No formatting applied. Added POM, java and resources folders. * Moved project from roo to spring-roo folder. * BAEL-838 Initial code showing how to remove last char - helper class and tests. * BAEL-838 Corrected Helper class and associated empty string test case. Added StringUtils.substing tests. * BAEL-838 Refromatted code using formatter.xml. Added Assert.assertEquals import. Renamed test to follow convention. Reordered tests. * BAEL-838 - Added regex method and updated tests. * BAEL-838 Added new line examples. * BAEL-838 Renamed RemoveLastChar class to StringHelper and added Java8 examples. Refactord code. * BAEL-838 Changed method names * BAEL-838 Tiny change to keep code consistant. Return null or empty. * BAEL-838 Removed unresolved conflict. * BAEL-821 New class that shows different rounding techniques. Updated POM. * BAEL-821 - Added unit test for different round methods. * BAEL-821 Changed test method name to follow the convention * BAEL-821 Added more test and updated round methods. * BAEL-837 - initial commit. A few examples of adding doubles. * BAEL-837 - Couple of smaller changes * BAEL-837 - Added jUnit test. * BAEL-579 Updated Spring Cloud Version I was getting error: java.lang.NoSuchMethodError: org.springframework.cloud.config.environment.Environment After version update, all is okay. * BAEL-579 Added actuator to Cloud Config Client. * BAEL-579 Enabled cloud bus and updated dependencies. * BAEL-579 Config Client using Spring Cloud Bus. * BAEL-579 Recreated Basic Config Server. * BAEL-579 Recreated Config Client. * BAEL-579 Removed test Git URL. * BAEL-579 Added Actuator to Config Client * BAEL-579 Added Spring Cloud Bus to Client. * BAEL-579 Server changes for Spring Cloud Bus Added dependencies and removed git.clone-on-start as this was causing server to throw errors after git properties change. * BAEL-579 Removed Git URL. * Revert "BAEL-579 Updated Spring Cloud Version" This reverts commit f775bf91e53a1ecfb9b70596688d7c8202bf495f. * Revert "BAEL-579 Config Client using Spring Cloud Bus." This reverts commit 1d96bc5761994a33af9a7a9aa5ab68604a5b44dc. * Revert "BAEL-579 Enabled cloud bus and updated dependencies." This reverts commit 7845da922d89d53506dd0fff387ea13694c50bc1. * Revert "BAEL-579 Added actuator to Cloud Config Client." This reverts commit 076657a26a57e0aa676989a4d97966a3b9d53e1c. * BAEL-579 Added missing dependency versions. * BAEL-579 Added missing dependency versions. * Updated gitignore * BAEL-1065 Simple performance check StringBuffer vs StringBuilder. * BAEL-1065 Added JMH benchmarks --- .gitignore | 3 +- core-java/pom.xml | 27 ++++++++++- .../string/StringBufferStringBuilder.java | 46 +++++++++++++++++++ .../java/com/baeldung/maths/RoundTest.java | 24 +++++----- 4 files changed, 86 insertions(+), 14 deletions(-) create mode 100644 core-java/src/main/java/com/baeldung/string/StringBufferStringBuilder.java diff --git a/.gitignore b/.gitignore index a8f09b4135..08f570ad06 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties -/vertx-and-rxjava/.vertx/ +*.springBeans + diff --git a/core-java/pom.xml b/core-java/pom.xml index d4d249934b..0e57b35ab8 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -372,6 +372,31 @@ + + + org.codehaus.mojo + exec-maven-plugin + + + + run-benchmarks + integration-test + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + @@ -448,4 +473,4 @@ 3.6.0 2.19.1 - + \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/string/StringBufferStringBuilder.java b/core-java/src/main/java/com/baeldung/string/StringBufferStringBuilder.java new file mode 100644 index 0000000000..74f489d57f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/StringBufferStringBuilder.java @@ -0,0 +1,46 @@ +package com.baeldung.string; + +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.Scope; +import org.openjdk.jmh.annotations.State; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; + +public class StringBufferStringBuilder { + + public static void main(String[] args) throws RunnerException { + + Options opt = new OptionsBuilder() + .include(StringBufferStringBuilder.class.getSimpleName()) + .build(); + + new Runner(opt).run(); + } + + @State(Scope.Benchmark) + public static class MyState { + int iterations = 1000; + String initial = "abc"; + String suffix = "def"; + } + + @Benchmark + public StringBuffer benchmarkStringBuffer(MyState state) { + StringBuffer stringBuffer = new StringBuffer(state.initial); + for (int i = 0; i < state.iterations; i++) { + stringBuffer.append(state.suffix); + } + return stringBuffer; + } + + @Benchmark + public StringBuilder benchmarkStringBuilder(MyState state) { + StringBuilder stringBuilder = new StringBuilder(state.initial); + for (int i = 0; i < state.iterations; i++) { + stringBuilder.append(state.suffix); + } + return stringBuilder; + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/maths/RoundTest.java b/core-java/src/test/java/com/baeldung/maths/RoundTest.java index 9e3c049c96..5ce9523e21 100644 --- a/core-java/src/test/java/com/baeldung/maths/RoundTest.java +++ b/core-java/src/test/java/com/baeldung/maths/RoundTest.java @@ -15,56 +15,56 @@ public class RoundTest { private double expected = 2.03d; @Test - public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() { + public void givenDecimalNumber_whenRoundToNDecimalPlaces_thenGetExpectedResult() { Assert.assertEquals(expected, Round.round(value, places), delta); Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta); Assert.assertEquals(expected, Round.roundAvoid(value, places), delta); Assert.assertEquals(expected, Precision.round(value, places), delta); Assert.assertEquals(expected, DoubleRounder.round(value, places), delta); - + places = 3; expected = 2.035d; - + Assert.assertEquals(expected, Round.round(value, places), delta); Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta); Assert.assertEquals(expected, Round.roundAvoid(value, places), delta); Assert.assertEquals(expected, Precision.round(value, places), delta); Assert.assertEquals(expected, DoubleRounder.round(value, places), delta); - + value = 1000.0d; places = 17; expected = 1000.0d; - + Assert.assertEquals(expected, Round.round(value, places), delta); Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta); Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 92.23372036854776 ! Assert.assertEquals(expected, Precision.round(value, places), delta); Assert.assertEquals(expected, DoubleRounder.round(value, places), delta); - + value = 256.025d; places = 2; expected = 256.03d; - + Assert.assertEquals(expected, Round.round(value, places), delta); Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 256.02 ! Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 256.02 ! Assert.assertEquals(expected, Precision.round(value, places), delta); Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 256.02 ! - - value = 260.775d; + + value = 260.775d; places = 2; expected = 260.78d; - + Assert.assertEquals(expected, Round.round(value, places), delta); Assert.assertNotEquals(expected, Round.roundNotPrecise(value, places), delta); // Returns: 260.77 ! Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 260.77 ! Assert.assertEquals(expected, Precision.round(value, places), delta); Assert.assertNotEquals(expected, DoubleRounder.round(value, places), delta); // Returns: 260.77 ! - + value = 90080070060.1d; places = 9; expected = 90080070060.1d; - + Assert.assertEquals(expected, Round.round(value, places), delta); Assert.assertEquals(expected, Round.roundNotPrecise(value, places), delta); Assert.assertNotEquals(expected, Round.roundAvoid(value, places), delta); // Returns: 9.223372036854776E9 !