Examples for Branch Prediction article (#8426)

This commit is contained in:
Graham Cox 2019-12-28 19:20:11 +00:00 committed by Grzegorz Piwowarek
parent f4ff0da44a
commit 17dc359d5b
3 changed files with 209 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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<Long> 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);
}
}

View File

@ -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<Long> 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);
}
}