Skip to content

Commit f06c23c

Browse files
sazzerpivovarit
authored andcommitted
Examples for Branch Prediction article (eugenp#8426)
1 parent 6503d96 commit f06c23c

3 files changed

Lines changed: 209 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.branchprediction;
2+
3+
import java.util.stream.LongStream;
4+
5+
import org.junit.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
public class CombiningUnitTest {
10+
private static final Logger LOG = LoggerFactory.getLogger(CombiningUnitTest.class);
11+
12+
public static final int TOP = 10000000;
13+
public static final double FRACTION = 0.1;
14+
15+
@Test
16+
public void combined() {
17+
long[] first = LongStream.range(0, TOP)
18+
.map(n -> Math.random() < FRACTION ? 0 : n)
19+
.toArray();
20+
long[] second = LongStream.range(0, TOP)
21+
.map(n -> Math.random() < FRACTION ? 0 : n)
22+
.toArray();
23+
24+
long count = 0;
25+
long start = System.currentTimeMillis();
26+
for (int i = 0; i < TOP; i++) {
27+
if (first[i] * second[i] != 0) {
28+
++count;
29+
}
30+
}
31+
long end = System.currentTimeMillis();
32+
33+
LOG.info("Counted {}/{} numbers using combined mode in {}ms", count, TOP, end - start);
34+
35+
}
36+
37+
@Test
38+
public void separate() {
39+
long[] first = LongStream.range(0, TOP)
40+
.map(n -> Math.random() < FRACTION ? 0 : n)
41+
.toArray();
42+
long[] second = LongStream.range(0, TOP)
43+
.map(n -> Math.random() < FRACTION ? 0 : n)
44+
.toArray();
45+
46+
long count = 0;
47+
long start = System.currentTimeMillis();
48+
for (int i = 0; i < TOP; i++) {
49+
if (first[i] != 0 && second[i] != 0) {
50+
++count;
51+
}
52+
}
53+
long end = System.currentTimeMillis();
54+
55+
LOG.info("Counted {}/{} numbers using separate mode in {}ms", count, TOP, end - start);
56+
}
57+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.branchprediction;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.LongStream;
7+
8+
import org.junit.Test;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
public class IfUnitTest {
13+
private static final Logger LOG = LoggerFactory.getLogger(IfUnitTest.class);
14+
15+
public static final int TOP = 10000000;
16+
17+
@Test
18+
public void majorBranchSorted() {
19+
test(TOP, 0.9, false);
20+
}
21+
22+
@Test
23+
public void minorBranchSorted() {
24+
test(TOP, 0.1, false);
25+
}
26+
27+
@Test
28+
public void equalBranchSorted() {
29+
test(TOP, 0.5, false);
30+
}
31+
32+
@Test
33+
public void allBranchSorted() {
34+
test(TOP, 1, false);
35+
}
36+
37+
@Test
38+
public void noneBranchSorted() {
39+
test(TOP, 0, false);
40+
}
41+
42+
@Test
43+
public void majorBranchShuffled() {
44+
test(TOP, 0.9, true);
45+
}
46+
47+
@Test
48+
public void minorBranchShuffled() {
49+
test(TOP, 0.1, true);
50+
}
51+
52+
@Test
53+
public void equalBranchShuffled() {
54+
test(TOP, 0.5, true);
55+
}
56+
57+
@Test
58+
public void allBranchShuffled() {
59+
test(TOP, 1, true);
60+
}
61+
62+
@Test
63+
public void noneBranchShuffled() {
64+
test(TOP, 0, true);
65+
}
66+
67+
private void test(long top, double cutoffPercentage, boolean shuffle) {
68+
List<Long> numbers = LongStream.range(0, top)
69+
.boxed()
70+
.collect(Collectors.toList());
71+
if (shuffle) {
72+
Collections.shuffle(numbers);
73+
}
74+
75+
long cutoff = (long)(top * cutoffPercentage);
76+
long low = 0;
77+
long high = 0;
78+
79+
long start = System.currentTimeMillis();
80+
for (Long number : numbers) {
81+
if (number < cutoff) {
82+
++low;
83+
} else {
84+
++high;
85+
}
86+
}
87+
long end = System.currentTimeMillis();
88+
89+
LOG.info("Counted {}/{} {} numbers in {}ms", low, high, shuffle ? "shuffled" : "sorted", end - start);
90+
91+
}
92+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.branchprediction;
2+
3+
import java.util.Collections;
4+
import java.util.List;
5+
import java.util.stream.Collectors;
6+
import java.util.stream.LongStream;
7+
8+
import org.junit.Test;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
public class SortingUnitTest {
13+
private static final Logger LOG = LoggerFactory.getLogger(SortingUnitTest.class);
14+
public static final int BIG = 10000000;
15+
public static final int SMALL = 100000;
16+
17+
@Test
18+
public void sortedBig() {
19+
test(BIG, false);
20+
}
21+
22+
@Test
23+
public void shuffledBig() {
24+
test(BIG, true);
25+
}
26+
27+
@Test
28+
public void sortedSmall() {
29+
test(SMALL, false);
30+
}
31+
32+
@Test
33+
public void shuffledSmall() {
34+
test(SMALL, true);
35+
}
36+
37+
private void test(long top, boolean shuffle) {
38+
List<Long> numbers = LongStream.range(0, top)
39+
.boxed()
40+
.collect(Collectors.toList());
41+
42+
if (shuffle) {
43+
Collections.shuffle(numbers);
44+
}
45+
46+
long cutoff = top / 2;
47+
long count = 0;
48+
49+
long start = System.currentTimeMillis();
50+
for (Long number : numbers) {
51+
if (number < cutoff) {
52+
++count;
53+
}
54+
}
55+
long end = System.currentTimeMillis();
56+
57+
LOG.info("Counted {}/{} {} numbers in {}ms",
58+
count, top, shuffle ? "shuffled" : "sorted", end - start);
59+
}
60+
}

0 commit comments

Comments
 (0)