diff --git a/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/CombiningUnitTest.java b/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/CombiningUnitTest.java new file mode 100644 index 0000000000..c98eef5ed1 --- /dev/null +++ b/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/CombiningUnitTest.java @@ -0,0 +1,57 @@ +package com.baeldung.branchprediction; + +import java.util.stream.LongStream; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CombiningUnitTest { + private static final Logger LOG = LoggerFactory.getLogger(CombiningUnitTest.class); + + public static final int TOP = 10000000; + public static final double FRACTION = 0.1; + + @Test + public void combined() { + long[] first = LongStream.range(0, TOP) + .map(n -> Math.random() < FRACTION ? 0 : n) + .toArray(); + long[] second = LongStream.range(0, TOP) + .map(n -> Math.random() < FRACTION ? 0 : n) + .toArray(); + + long count = 0; + long start = System.currentTimeMillis(); + for (int i = 0; i < TOP; i++) { + if (first[i] * second[i] != 0) { + ++count; + } + } + long end = System.currentTimeMillis(); + + LOG.info("Counted {}/{} numbers using combined mode in {}ms", count, TOP, end - start); + + } + + @Test + public void separate() { + long[] first = LongStream.range(0, TOP) + .map(n -> Math.random() < FRACTION ? 0 : n) + .toArray(); + long[] second = LongStream.range(0, TOP) + .map(n -> Math.random() < FRACTION ? 0 : n) + .toArray(); + + long count = 0; + long start = System.currentTimeMillis(); + for (int i = 0; i < TOP; i++) { + if (first[i] != 0 && second[i] != 0) { + ++count; + } + } + long end = System.currentTimeMillis(); + + LOG.info("Counted {}/{} numbers using separate mode in {}ms", count, TOP, end - start); + } +} diff --git a/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/IfUnitTest.java b/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/IfUnitTest.java new file mode 100644 index 0000000000..6f80624502 --- /dev/null +++ b/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/IfUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.branchprediction; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.LongStream; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class IfUnitTest { + private static final Logger LOG = LoggerFactory.getLogger(IfUnitTest.class); + + public static final int TOP = 10000000; + + @Test + public void majorBranchSorted() { + test(TOP, 0.9, false); + } + + @Test + public void minorBranchSorted() { + test(TOP, 0.1, false); + } + + @Test + public void equalBranchSorted() { + test(TOP, 0.5, false); + } + + @Test + public void allBranchSorted() { + test(TOP, 1, false); + } + + @Test + public void noneBranchSorted() { + test(TOP, 0, false); + } + + @Test + public void majorBranchShuffled() { + test(TOP, 0.9, true); + } + + @Test + public void minorBranchShuffled() { + test(TOP, 0.1, true); + } + + @Test + public void equalBranchShuffled() { + test(TOP, 0.5, true); + } + + @Test + public void allBranchShuffled() { + test(TOP, 1, true); + } + + @Test + public void noneBranchShuffled() { + test(TOP, 0, true); + } + + private void test(long top, double cutoffPercentage, boolean shuffle) { + List numbers = LongStream.range(0, top) + .boxed() + .collect(Collectors.toList()); + if (shuffle) { + Collections.shuffle(numbers); + } + + long cutoff = (long)(top * cutoffPercentage); + long low = 0; + long high = 0; + + long start = System.currentTimeMillis(); + for (Long number : numbers) { + if (number < cutoff) { + ++low; + } else { + ++high; + } + } + long end = System.currentTimeMillis(); + + LOG.info("Counted {}/{} {} numbers in {}ms", low, high, shuffle ? "shuffled" : "sorted", end - start); + + } +} diff --git a/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/SortingUnitTest.java b/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/SortingUnitTest.java new file mode 100644 index 0000000000..6af40bd57a --- /dev/null +++ b/core-java-modules/core-java-perf/src/test/java/com/baeldung/branchprediction/SortingUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.branchprediction; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.LongStream; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SortingUnitTest { + private static final Logger LOG = LoggerFactory.getLogger(SortingUnitTest.class); + public static final int BIG = 10000000; + public static final int SMALL = 100000; + + @Test + public void sortedBig() { + test(BIG, false); + } + + @Test + public void shuffledBig() { + test(BIG, true); + } + + @Test + public void sortedSmall() { + test(SMALL, false); + } + + @Test + public void shuffledSmall() { + test(SMALL, true); + } + + private void test(long top, boolean shuffle) { + List numbers = LongStream.range(0, top) + .boxed() + .collect(Collectors.toList()); + + if (shuffle) { + Collections.shuffle(numbers); + } + + long cutoff = top / 2; + long count = 0; + + long start = System.currentTimeMillis(); + for (Long number : numbers) { + if (number < cutoff) { + ++count; + } + } + long end = System.currentTimeMillis(); + + LOG.info("Counted {}/{} {} numbers in {}ms", + count, top, shuffle ? "shuffled" : "sorted", end - start); + } +}