Skip to content

Commit 7f149a8

Browse files
committed
Committing before code review.
1 parent 39850ec commit 7f149a8

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ public final class ParallelRadixSort {
4747
* The array slices smaller than this number of elements will be sorted with
4848
* merge sort.
4949
*/
50-
static final int DEFAULT_MERGESORT_THRESHOLD = 4001;
50+
static final int DEFAULT_MERGESORT_THRESHOLD = 307;
5151

5252
/**
5353
* The array slices smaller than this number of elements will be sorted with
5454
* insertion sort.
5555
*/
56-
static final int DEFAULT_INSERTION_SORT_THRESHOLD = 13;
56+
static final int DEFAULT_INSERTION_SORT_THRESHOLD = 17;
5757

5858
/**
5959
* The minimum workload for a thread.
@@ -63,7 +63,7 @@ public final class ParallelRadixSort {
6363
/**
6464
* Minimum merge sort threshold.
6565
*/
66-
private static final int MINIMUM_MERGESORT_THRESHOLD = 1;
66+
private static final int MINIMUM_MERGESORT_THRESHOLD = 100;
6767

6868
/**
6969
* Minimum insertion sort threshold.
@@ -73,7 +73,7 @@ public final class ParallelRadixSort {
7373
/**
7474
* Minimum thread workload.
7575
*/
76-
private static final int MINIMUM_THREAD_WORKLOAD = 1;
76+
private static final int MINIMUM_THREAD_WORKLOAD = 14047;
7777

7878
/**
7979
* The current actual threshold for the insertion sort.
@@ -152,6 +152,7 @@ public static void parallelSort(int[] array, int fromIndex, int toIndex) {
152152
int rangeLength = toIndex - fromIndex;
153153

154154
if (rangeLength < 2) {
155+
// Trivially sorted, return.
155156
return;
156157
}
157158

@@ -287,9 +288,7 @@ private static void parallelRadixSortImpl(
287288

288289
int[][] processedMaps = new int[spawnDegree][BUCKETS];
289290

290-
// On linear data, all processedMaps[i][j] must be 0!
291-
292-
// Make the preprocessing map independent of each thread:
291+
// Make the preprocessing maps independent of each thread:
293292
for (int i = 1; i != spawnDegree; i++) {
294293
int[] partialBucketSizeMap =
295294
bucketSizeCounterThreads[i - 1].getLocalBucketSizeMap();

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

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ final class ParallelRadixSortBenchmark {
1212
private static final int MAXIMUM_SKIP_LAST_ELEMENTS = 1711;
1313

1414
public static void main(String[] args) {
15-
System.out.println("Warming up...");
15+
System.out.println("Warming up benchmark 1...");
1616
benchmark(false);
17-
System.out.println("Benchmarking...");
17+
System.out.println("Warming up benchmark 2...");
18+
benchmark2(false);
19+
System.out.println("Benchmarking 1...");
1820
benchmark(true);
21+
System.out.println("Benchmarking 2...");
22+
benchmark2(true);
1923
System.out.println("Benchmark done!");
2024
}
2125

@@ -25,7 +29,6 @@ private static void benchmark(boolean print) {
2529
long totalDuration2 = 0L;
2630

2731
for (int iteration = 0; iteration < BENCHMARK_ITERATIONS; iteration++) {
28-
System.gc();
2932

3033
int arrayLength =
3134
MINIMUM_ARRAY_SIZE +-
@@ -71,4 +74,56 @@ private static void benchmark(boolean print) {
7174
+ totalDuration2);
7275
}
7376
}
77+
78+
private static void benchmark2(boolean print) {
79+
Random random = new Random();
80+
long totalDuration1 = 0L;
81+
long totalDuration2 = 0L;
82+
83+
for (int iteration = 0; iteration < BENCHMARK_ITERATIONS; iteration++) {
84+
85+
int arrayLength =
86+
MINIMUM_ARRAY_SIZE +-
87+
random.nextInt(MAXIMUM_ARRAY_SIZE - MINIMUM_ARRAY_SIZE + 1);
88+
89+
int[] array1 = new int[arrayLength];
90+
int[] array2 = array1.clone();
91+
92+
int fromIndex = random.nextInt(MAXIMUM_FROM_INDEX + 1);
93+
int toIndex = array1.length -
94+
random.nextInt(MAXIMUM_SKIP_LAST_ELEMENTS + 1);
95+
96+
long startTime = System.currentTimeMillis();
97+
Arrays.parallelSort(array1, fromIndex, toIndex);
98+
long endTime = System.currentTimeMillis();
99+
long duration1 = endTime - startTime;
100+
totalDuration1 += duration1;
101+
102+
startTime = System.currentTimeMillis();
103+
ParallelRadixSort.parallelSort(array2, fromIndex, toIndex);
104+
endTime = System.currentTimeMillis();
105+
long duration2 = endTime - startTime;
106+
totalDuration2 += duration2;
107+
108+
boolean agreed = Arrays.equals(array1, array2);
109+
110+
if (print) {
111+
System.out.println(
112+
"Arrays.parallelSort: "
113+
+ duration1
114+
+ " ms, ParallelRadixSort.parallelSort: "
115+
+ duration2
116+
+ " ms, agreed: "
117+
+ agreed);
118+
}
119+
}
120+
121+
if (print) {
122+
System.out.println(
123+
"Total Arrays.parallelSort duration: "
124+
+ totalDuration1
125+
+ ", total ParallelRadixSort.parallelSort: "
126+
+ totalDuration2);
127+
}
128+
}
74129
}

0 commit comments

Comments
 (0)