string vs stringBuffer comparison (#15306)

Co-authored-by: Sahil <sahilsriv05@gmail.com>
This commit is contained in:
Sameer 2023-11-28 03:47:00 +05:30 committed by GitHub
parent 17705cd105
commit 6164b9c0ba
5 changed files with 145 additions and 2 deletions

View File

@ -0,0 +1,63 @@
package com.baeldung.stringbuffer;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
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;
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 10000, iterations = 10)
@Warmup(batchSize = 1000, iterations = 10)
@State(Scope.Thread)
public class ComparePerformance {
String strInitial = "springframework";
String strFinal = "";
String replacement = "java-";
@Benchmark
public String benchmarkStringConcatenation() {
strFinal += strInitial;
return strFinal;
}
@Benchmark
public StringBuffer benchmarkStringBufferConcatenation() {
StringBuffer stringBuffer = new StringBuffer(strFinal);
stringBuffer.append(strInitial);
return stringBuffer;
}
@Benchmark
public String benchmarkStringReplacement() {
strFinal = strInitial.replaceFirst("spring", replacement);
return strFinal;
}
@Benchmark
public StringBuffer benchmarkStringBufferReplacement() {
StringBuffer stringBuffer = new StringBuffer(strInitial);
stringBuffer.replace(0,6, replacement);
return stringBuffer;
}
public static void main(String[] args) throws RunnerException {
Options options = new OptionsBuilder()
.include(ComparePerformance.class.getSimpleName()).threads(1)
.forks(1).shouldFailOnError(true)
.shouldDoGC(true)
.jvmArgs("-server").build();
new Runner(options).run();
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.stringbuffer;
public class HashCode {
public static long getHashCodeString(String string) {
return string.hashCode();
}
public static long getHashCodeSBuffer(StringBuffer strBuff) {
return strBuff.hashCode();
}
public static void main(String[] args) {
String str = "Spring";
System.out.println("String HashCode pre concatenation :" + getHashCodeString(str));
str += "Framework";
System.out.println("String HashCode post concatenation :" + getHashCodeString(str));
StringBuffer sBuf = new StringBuffer("Spring");
System.out.println("StringBuffer HashCode pre concatenation :" + getHashCodeSBuffer(sBuf));
sBuf.append("Framework");
System.out.println("StringBuffer HashCode post concatenation :" + getHashCodeSBuffer(sBuf));
}
}

View File

@ -17,7 +17,7 @@ public class StringIteratorTest {
public void whenUseJavaForLoop_thenIterate() { public void whenUseJavaForLoop_thenIterate() {
String input = "Hello, Baeldung!"; String input = "Hello, Baeldung!";
String expectedOutput = "Hello, Baeldung!"; String expectedOutput = "Hello, Baeldung!";
String result = StringIterator.javaForLoop(input); String result = StringIterator.javaforLoop(input);
assertEquals(expectedOutput, result); assertEquals(expectedOutput, result);
} }
@ -25,7 +25,7 @@ public class StringIteratorTest {
public void whenUseForEachMethod_thenIterate() { public void whenUseForEachMethod_thenIterate() {
String input = "Hello, Baeldung!"; String input = "Hello, Baeldung!";
String expectedOutput = "Hello, Baeldung!"; String expectedOutput = "Hello, Baeldung!";
String result = StringIterator.java8ForEach(input); String result = StringIterator.java8forEach(input);
assertEquals(expectedOutput, result); assertEquals(expectedOutput, result);
} }

View File

@ -0,0 +1,32 @@
package com.baeldung.stringbuffer;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ComparePerformanceTest {
ComparePerformance cp = new ComparePerformance();
@Test
public void whenStringConcatenated_thenResultAsExpected() {
assertThat(cp.benchmarkStringConcatenation()).isEqualTo("springframework");
}
@Test
public void whenStringBufferConcatenated_thenResultAsExpected() {
StringBuffer stringBuffer = new StringBuffer("springframework");
assertThat(cp.benchmarkStringBufferConcatenation()).isEqualToIgnoringCase(stringBuffer);
}
@Test
public void whenStringReplaced_thenResultAsExpected() {
assertThat(cp.benchmarkStringReplacement()).isEqualTo("java-framework");
}
@Test
public void whenStringBufferReplaced_thenResultAsExpected() {
StringBuffer stringBuffer = new StringBuffer("java-framework");
assertThat(cp.benchmarkStringBufferReplacement()).isEqualToIgnoringCase(stringBuffer);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.stringbuffer;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class HashCodeTest {
String str = "Spring";
StringBuffer sBuf = new StringBuffer("Spring");
@Test
public void whenStringConcat_thenHashCodeChanges() {
HashCode hc = new HashCode();
long initialStringHashCode = hc.getHashCodeString(str);
long initialSBufHashCode = hc.getHashCodeSBuffer(sBuf);
str += "Framework";
sBuf.append("Framework");
assertThat(initialStringHashCode).isNotEqualTo(hc.getHashCodeString(str));
assertThat(initialSBufHashCode).isEqualTo(hc.getHashCodeSBuffer(sBuf));
}
}