Skip to content

Commit 675d57d

Browse files
committed
The algorithm passes the unit tests and added the benchmark class.
1 parent 7b7a3a5 commit 675d57d

File tree

4 files changed

+102
-32
lines changed

4 files changed

+102
-32
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
nbproject
2-
target
2+
target
3+
nbactions.xml

src/main/java/com/github/coderodde/util/ParallelRadixSort.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,18 +356,18 @@ private static void parallelRadixSortImpl(
356356
}
357357
}
358358

359-
int[] testArray = source.clone();
360-
361-
Arrays.sort(testArray);
362-
363-
for (int i = 0; i != testArray.length; i++) {
364-
int sourceElement = target[i];
365-
int testElement = testArray[i];
366-
367-
if (sourceElement != testElement) {
368-
System.out.println("DEBUG: " + sourceElement + " vs. " + testElement + " at index " + i);
369-
}
370-
}
359+
// int[] testArray = source.clone();
360+
//
361+
// Arrays.sort(testArray);
362+
//
363+
// for (int i = 0; i != testArray.length; i++) {
364+
// int sourceElement = target[i];
365+
// int testElement = testArray[i];
366+
//
367+
// if (sourceElement != testElement) {
368+
// System.out.println("DEBUG: " + sourceElement + " vs. " + testElement + " at index " + i);
369+
// }
370+
// }
371371

372372
if (recursionDepth == DEEPEST_RECURSION_DEPTH) {
373373
// Nowhere to recur, all bytes are processed. Return.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.github.coderodde.util;
2+
3+
import java.util.Arrays;
4+
import java.util.Random;
5+
6+
final class ParallelRadixSortBenchmark {
7+
8+
private static final int BENCHMARK_ITERATIONS = 20;
9+
private static final int MAXIMUM_ARRAY_SIZE = 50_000_000;
10+
private static final int MINIMUM_ARRAY_SIZE = 40_000_000;
11+
private static final int MAXIMUM_FROM_INDEX = 1313;
12+
private static final int MAXIMUM_SKIP_LAST_ELEMENTS = 1711;
13+
14+
public static void main(String[] args) {
15+
System.out.println("Warming up...");
16+
benchmark(false);
17+
System.out.println("Benchmarking...");
18+
benchmark(true);
19+
System.out.println("Benchmark done!");
20+
}
21+
22+
private static void benchmark(boolean print) {
23+
Random random = new Random();
24+
long totalDuration1 = 0L;
25+
long totalDuration2 = 0L;
26+
27+
for (int iteration = 0; iteration < BENCHMARK_ITERATIONS; iteration++) {
28+
int arrayLength =
29+
MINIMUM_ARRAY_SIZE +-
30+
random.nextInt(MAXIMUM_ARRAY_SIZE - MINIMUM_ARRAY_SIZE + 1);
31+
32+
int[] array1 = Utils.createRandomIntArray(arrayLength, random);
33+
int[] array2 = array1.clone();
34+
35+
int fromIndex = random.nextInt(MAXIMUM_FROM_INDEX + 1);
36+
int toIndex = array1.length -
37+
random.nextInt(MAXIMUM_SKIP_LAST_ELEMENTS + 1);
38+
39+
long startTime = System.currentTimeMillis();
40+
Arrays.parallelSort(array1, fromIndex, toIndex);
41+
long endTime = System.currentTimeMillis();
42+
long duration1 = endTime - startTime;
43+
totalDuration1 += duration1;
44+
45+
startTime = System.currentTimeMillis();
46+
ParallelRadixSort.parallelSort(array2, fromIndex, toIndex);
47+
endTime = System.currentTimeMillis();
48+
long duration2 = endTime - startTime;
49+
totalDuration2 += duration2;
50+
51+
boolean agreed = Arrays.equals(array1, array2);
52+
53+
if (print) {
54+
System.out.println(
55+
"Arrays.parallelSort: "
56+
+ duration1
57+
+ " ms, ParallelRadixSort.parallelSort: "
58+
+ duration2
59+
+ ", agreed: "
60+
+ agreed);
61+
}
62+
}
63+
64+
if (print) {
65+
System.out.println(
66+
"Total Arrays.parallelSort duration: "
67+
+ totalDuration1
68+
+ ", total ParallelRadixSort.parallelSort: "
69+
+ totalDuration2);
70+
}
71+
}
72+
}

src/test/java/com/github/coderodde/util/ParallelRadixSortTest.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ public final class ParallelRadixSortTest {
1111

1212
@BeforeClass
1313
public static void setupBeforeClass() {
14-
ParallelRadixSort.setInsertionSortThreshold(-1);
15-
ParallelRadixSort.setMergesortThreshold(-1);
16-
ParallelRadixSort.setMinimumThreadWorkload(-1);
14+
// ParallelRadixSort.setInsertionSortThreshold(-1);
15+
// ParallelRadixSort.setMergesortThreshold(-1);
16+
// ParallelRadixSort.setMinimumThreadWorkload(-1);
1717
}
1818

19-
// @Test
19+
@Test
2020
public void testInsertionSort() {
2121
Random random = new Random(13L);
2222

@@ -33,7 +33,7 @@ public void testInsertionSort() {
3333
assertTrue(Arrays.equals(array1, array2));
3434
}
3535

36-
// @Test
36+
@Test
3737
public void testMergesort() {
3838
Random random = new Random(123L);
3939

@@ -54,7 +54,7 @@ public void testMergesort() {
5454
assertTrue(Arrays.equals(array1, array2));
5555
}
5656

57-
// @Test
57+
@Test
5858
public void testSerialRadixSort() {
5959
Random random = new Random(26);
6060

@@ -84,21 +84,18 @@ public void testSerialRadixSort() {
8484
public void testParallelRadixSort() {
8585
Random random = new Random(29);
8686

87-
final int SIZE = 256;
87+
final int SIZE = 5_000_000;
88+
final int FROM_INDEX = 13;
89+
final int TO_INDEX = SIZE - 17;
8890

89-
int[] array1 = Utils.createLinearDebugIntArray(SIZE, random);
90-
// int[] array1 = Utils.createRandomIntArray(
91-
// SIZE,
92-
// Integer.MAX_VALUE - 1,
93-
// random);
91+
int[] array1 =
92+
Utils.createRandomIntArray(
93+
SIZE,
94+
Integer.MAX_VALUE,
95+
random);
9496

9597
int[] array2 = array1.clone();
9698

97-
// final int FROM_INDEX = 13;
98-
// final int TO_INDEX = SIZE - 15;
99-
final int FROM_INDEX = 0;
100-
final int TO_INDEX = SIZE;
101-
10299
Arrays.sort(array1, FROM_INDEX, TO_INDEX);
103100
ParallelRadixSort.parallelSort(
104101
array2,
@@ -108,7 +105,7 @@ public void testParallelRadixSort() {
108105
assertTrue(Arrays.equals(array1, array2));
109106
}
110107

111-
// @Test
108+
@Test
112109
public void bruteForceTestInsertionsort() {
113110
final int ITERATIONS = 200;
114111
Random random = new Random(432);
@@ -153,7 +150,7 @@ public void bruteForceTestInsertionsort() {
153150
}
154151
}
155152

156-
// @Test
153+
@Test
157154
public void bruteForceTestMergesort() {
158155
final int ITERATIONS = 200;
159156
Random random = new Random(3);

0 commit comments

Comments
 (0)