Skip to content

Commit

Permalink
Add averaging
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaming32 committed Nov 2, 2021
1 parent c2034e7 commit 136c244
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 30 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/benchmark_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Test sorts w/ better benchmark
on:
workflow_dispatch:
inputs:
averageCount:
description: The number of times to average a run
required: true
default: "3"

jobs:
bench_java:
runs-on: macos-latest # Use macOS because it gives us more memory (14 GB instead of 7 GB)
defaults:
run:
working-directory: "Holy Grail Sort/Java/Summer Dragonfly et al.'s Rough Draft"
steps:
- uses: actions/checkout@v2
- name: Setup Java
uses: actions/[email protected]
with:
java-version: 8
distribution: zulu
- name: Build Tester
run: javac -d bin src/holygrail/*.java
- name: Checkout RGS
uses: actions/checkout@v2
with:
repository: HolyGrailSortProject/Rewritten-Grailsort
path: RGS
- name: Build RGS
run: javac -d bin "../../../RGS/Java/Summer Dragonfly et al.'s Rewritten Grailsort for Java/src/sort/GrailSort.java"
- name: Run Tester
run: java -Xmx12G -cp bin holygrail.Tester -average ${{ github.event.inputs.averageCount }}
7 changes: 0 additions & 7 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,5 @@ jobs:
distribution: zulu
- name: Build Tester
run: javac -d bin src/holygrail/*.java
- name: Checkout RGS
uses: actions/checkout@v2
with:
repository: HolyGrailSortProject/Rewritten-Grailsort
path: RGS
- name: Build RGS
run: javac -d bin "../../../RGS/Java/Summer Dragonfly et al.'s Rewritten Grailsort for Java/src/sort/GrailSort.java"
- name: Run Tester
run: java -Xmx12G -cp bin holygrail.Tester
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public int compare(GrailPair o1, GrailPair o2) {
}
}

private int averages;
private int seed;
private int maxLength, maxKeyCount;

Expand All @@ -116,8 +117,9 @@ public int compare(GrailPair o1, GrailPair o2) {
private String failReason;
private int count, successes, failures;

public Tester(int maxLength, int maxKeyCount) {
public Tester(int maxLength, int maxKeyCount, int averages) {
this.seed = 100000001;
this.averages = averages;
this.maxLength = maxLength;
this.maxKeyCount = maxKeyCount;
initArrays();
Expand Down Expand Up @@ -169,10 +171,40 @@ else if(!this.keyArray[i - 1].equals(this.referenceArray[i - 1])) {
return true;
}

private void checkAlgorithm(int start, int length, int keyCount, int algorithm, int grailBufferType, String grailStrategy, GrailComparator test) {
private long checkAlgorithmWithAverage(int start, int length, int keyCount, int algorithm, int grailBufferType, String grailStrategy, GrailComparator test) {
String grailType = "w/o External Buffer";
if(grailBufferType == 1) {
grailType = "w/ O(1) Buffer ";
}
else if(grailBufferType == 2) {
grailType = "w/ O(sqrt n) Buffer";
}

if(algorithm == 0) {
System.out.println("\n* Holy Grail Sort " + grailType + ", " + grailStrategy + " \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
} else if(algorithm == 1) {
System.out.println("\n* Rewritten Grailsort " + grailType + ", " + grailStrategy + " \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
} else {
System.out.println("\n* Arrays.sort (Tim Sort) \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
}

long sum = 0;
for (int n = 0; n < averages; n++) {
long oneTime = checkAlgorithm(start, length, keyCount, algorithm, grailBufferType, null, test);
if (oneTime != -1) sum += oneTime;
}
long time = sum / averages;
if (averages > 1) {
System.out.println("- Average sort time: " + time * 1e-6d + "ms");
}
return time;
}

private long checkAlgorithm(int start, int length, int keyCount, int algorithm, int grailBufferType, String grailStrategy, GrailComparator test) {
int tempSeed = this.seed;
long time = -1;
try {
checkAlgorithm0(start, length, keyCount, algorithm, grailBufferType, grailStrategy, test);
time = checkAlgorithm0(start, length, keyCount, algorithm, grailBufferType, grailStrategy, test);
} catch (OutOfMemoryError e) {
System.err.println("Warning: tester ran out of memory");
e.printStackTrace();
Expand All @@ -184,7 +216,7 @@ private void checkAlgorithm(int start, int length, int keyCount, int algorithm,
initArrays();
System.err.println("Re-running check...");
try {
checkAlgorithm0(start, length, keyCount, algorithm, grailBufferType, grailStrategy, test);
time = checkAlgorithm0(start, length, keyCount, algorithm, grailBufferType, grailStrategy, test);
} catch (Exception e1) {
System.out.println("Sort failed with exception:");
e.printStackTrace();
Expand All @@ -199,25 +231,28 @@ private void checkAlgorithm(int start, int length, int keyCount, int algorithm,
this.count++;
}
this.seed = tempSeed;
return time;
}

private void checkAlgorithm0(int start, int length, int keyCount, int algorithm, int grailBufferType, String grailStrategy, GrailComparator test) throws Exception {
private long checkAlgorithm0(int start, int length, int keyCount, int algorithm, int grailBufferType, String grailStrategy, GrailComparator test) throws Exception {
GrailPair[] array = algorithm == 2 ? this.referenceArray : this.keyArray;
this.generateTestArray(array, start, length, keyCount);

String grailType = "w/o External Buffer";
if(grailBufferType == 1) {
grailType = "w/ O(1) Buffer ";
}
else if(grailBufferType == 2) {
grailType = "w/ O(sqrt n) Buffer";
}
if (grailStrategy != null) {
String grailType = "w/o External Buffer";
if(grailBufferType == 1) {
grailType = "w/ O(1) Buffer ";
}
else if(grailBufferType == 2) {
grailType = "w/ O(sqrt n) Buffer";
}

if(algorithm == 0) {
System.out.println("\n* Holy Grail Sort " + grailType + ", " + grailStrategy + " \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
} else if(algorithm == 1) {
System.out.println("\n* Rewritten Grailsort " + grailType + ", " + grailStrategy + " \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
} else {
if(algorithm == 0) {
System.out.println("\n* Holy Grail Sort " + grailType + ", " + grailStrategy + " \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
} else if(algorithm == 1) {
System.out.println("\n* Rewritten Grailsort " + grailType + ", " + grailStrategy + " \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
}
} else if (algorithm == 2) {
System.out.println("\n* Arrays.sort (Tim Sort) \n* start = " + start + ", length = " + length + ", unique items = " + keyCount);
}

Expand Down Expand Up @@ -277,37 +312,57 @@ else if(grailBufferType == 2) {

Arrays.fill(this.keyArray, null);
System.gc();

return time;
}

private void checkBoth(int start, int length, int keyCount, String grailStrategy, GrailComparator test) {
this.checkAlgorithm(start, length, keyCount, 2, 0, null, test);

if(!grailStrategy.equals("Opti.Gnome")) {
for(int i = 0; i < 1; i++) {
this.checkAlgorithm(start, length, keyCount, 0, i, grailStrategy, test);
this.checkAlgorithmWithAverage(start, length, keyCount, 0, i, grailStrategy, test);
if (RewrittenGrailsort.RGS_CLASS != null) {
this.checkAlgorithm(start, length, keyCount, 1, i, grailStrategy, test);
this.checkAlgorithmWithAverage(start, length, keyCount, 1, i, grailStrategy, test);
}
}
}
else {
this.checkAlgorithm(start, length, keyCount, 0, 0, grailStrategy, test);
this.checkAlgorithmWithAverage(start, length, keyCount, 0, 0, grailStrategy, test);
if (RewrittenGrailsort.RGS_CLASS != null) {
this.checkAlgorithm(start, length, keyCount, 1, 0, grailStrategy, test);
this.checkAlgorithmWithAverage(start, length, keyCount, 1, 0, grailStrategy, test);
}
}

Arrays.fill(this.referenceArray, null);
System.gc();
}

private static String getJOpt(String[] args, String optName, String orDefault) {
optName = "-" + optName;
for (int i = 0; i < args.length - 1; i++) {
if (args[i].equals(optName)) {
return args[i + 1];
}
}
return orDefault;
}

private static String getJOpt(String[] args, String optName) {
return getJOpt(args, optName, null);
}

public static void main(String[] args) {
int maxLength = 50000000;
int maxKeyCount = 25000000;

Tester tester = new Tester(maxLength, maxKeyCount);
Tester tester = new Tester(maxLength, maxKeyCount, Integer.valueOf(getJOpt(args, "average", "1")));
GrailComparator testCompare = new GrailComparator();

if (tester.averages > 1) {
System.out.println("Average timings based off of " + tester.averages + " averages.");
}

System.out.println("Warming-up the JVM...");

for(int u = 5; u <= (maxLength / 100); u *= 10) {
Expand All @@ -317,7 +372,7 @@ public static void main(String[] args) {
tester.seed = tempSeed;
Arrays.sort(tester.referenceArray, 0, u, testCompare);
for(int i = 0; i < 1; i++) {
tester.checkAlgorithm(0, u, v - 1, 0, i, "All Strategies", testCompare);
tester.checkAlgorithmWithAverage(0, u, v - 1, 0, i, "All Strategies", testCompare);
}
}
}
Expand Down

0 comments on commit 136c244

Please sign in to comment.