Created
April 3, 2018 02:14
-
-
Save N-B-Kelly/0e98786be431ffb4a18417b09a121814 to your computer and use it in GitHub Desktop.
Testing performance of various int -> trinary -> character conversion conversions. 2^12 combinations should be printed out in order.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class combinations { | |
public static void main(String[] args) { | |
//split: chunk size | |
//sequential: print each item in sequence as calculated | |
//sequentialB: concat all items into single string and print | |
//merge: top down split generation | |
//thread: make a trillion threads and merge | |
//threadB: make exactly eight threads and do merge | |
int max = 531441; | |
int split = 100; | |
if(args.length == 0) | |
Sequential(max); | |
else if(args.length == 1) | |
System.out.print(SequentialB(max)); | |
else if (args.length == 2) | |
System.out.print(Merge(0, max, split)); | |
else if (args.length == 3) { | |
System.out.print(Thread(0, max, split)); | |
} | |
else if (args.length == 4) { | |
System.out.print(Thread2(0, max, split)); | |
} | |
} | |
private static StringBuilder Thread2(int min, int max, int split) { | |
int diff = (max-min)/8; | |
//my cpu has two cores that can handle four threads: this seems optimal | |
//I know you can set the threads based on core count programatically, and use a thread pool properly | |
//but I can't be bothered for this example | |
MyThreadB t1 = new MyThreadB(min, min + diff, split); | |
MyThreadB t2 = new MyThreadB(min, min + diff, split); | |
MyThreadB t3 = new MyThreadB(min, min + diff, split); | |
MyThreadB t4 = new MyThreadB(min, min + diff, split); | |
StringBuilder ans = new StringBuilder(); | |
try { | |
t1.join(); | |
ans.append(t1.res); | |
t2.join(); | |
ans.append(t2.res); | |
t3.join(); | |
ans.append(t3.res); | |
t4.join(); | |
ans.append(t4.res); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return ans; | |
} | |
private static StringBuilder Thread(int min, int max, int split) { | |
MyThread thread = new MyThread(min, max, split); | |
try { | |
thread.join(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return thread.res; | |
} | |
private static class MyThreadB extends Thread { | |
int min, max, split; | |
public StringBuilder res; | |
public MyThreadB(int min, int max, int split) { | |
this.min = min; | |
this.max = max; | |
this.split = split; | |
res = new StringBuilder(); | |
start(); | |
} | |
public void run() { | |
res = Merge(min, max, split); | |
} | |
} | |
private static class MyThread extends Thread { | |
int min, max, split; | |
public StringBuilder res; | |
public MyThread(int min, int max, int split) { | |
this.min = min; | |
this.max = max; | |
this.split = split; | |
res = new StringBuilder(); | |
start(); | |
} | |
public void run() { | |
int start = min; | |
int end = max; | |
int diff = max - min; | |
if(diff <= split) { | |
for(int i = start; i < end; i++) { | |
res.append(getStr(i)); | |
} | |
return; | |
} | |
int start2 = start + diff/2; | |
MyThread t1 = new MyThread(start, start2, split); | |
MyThread t2 = new MyThread(start2, end, split); | |
try { | |
t1.join(); | |
t2.join(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
res = t1.res.append(t2.res); | |
} | |
} | |
private static StringBuilder Merge(int start, int end, int minsize) { | |
int diff = end - start; | |
if (diff <= minsize) { | |
StringBuilder res = new StringBuilder(); | |
for(int i = start; i < end; i++) { | |
res.append(getStr(i)); | |
} | |
return res; | |
} | |
int start2 = start + diff/2; | |
StringBuilder left = Merge(start, start2, minsize); | |
StringBuilder right = Merge(start2, end, minsize); | |
return left.append(right); | |
} | |
private static String getStr(int val) { | |
return Trinary2Ans(Int2Trinary(val)) + "\n"; | |
} | |
//print out answers in sequence | |
private static void Sequential(int maxnum) { | |
for(int i = 0; i < maxnum; i++) { | |
System.out.println(Trinary2Ans(Int2Trinary(i))); | |
} | |
} | |
//return stringbuilder containing all answers, ready to print! | |
private static StringBuilder SequentialB(int maxnum) { | |
StringBuilder res = new StringBuilder(getStr(0)); | |
for(int i = 1; i < maxnum; i++) { | |
res.append(getStr(i)); | |
} | |
return res; | |
} | |
//THIS IS BAD, BUT IT'S STATIC ACROSS ALL EXPERIMENTS | |
private static String Trinary2Ans(long num) { | |
//make string length of max | |
String trinary = String.format("%012d", num); | |
trinary = trinary.replace('0','A'); | |
trinary = trinary.replace('1','B'); | |
trinary = trinary.replace('2','C'); | |
return trinary; | |
} | |
private static long Int2Trinary(int input) { | |
long res = 0, mul = 1; | |
while (input > 0) { | |
res += (input % 3) * mul; | |
input /= 3; | |
mul *= 10; | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo "Test One" | |
time java combinations > out.txt | |
echo "Test Two" | |
time java combinations 1 > out.txt | |
echo "Test Three" | |
time java combinations 1 1 > out.txt | |
echo "Test four" | |
time java combinations 1 1 1 > out.txt | |
echo "Test Five" | |
time java combinations 1 1 1 1 > out.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment