University stuff.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Main.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import java.util.Random;
  2. import java.util.Arrays;
  3. class Main {
  4. static void usage() {
  5. System.out.println("Usage: java Main <n> <k> [seq|par]");
  6. System.out.println(" or: java Main test");
  7. }
  8. static final int randSeed = 1337; // Just to keep things consistent
  9. static class Timer {
  10. long start = 0;
  11. long time = 0;
  12. void start() {
  13. start = System.nanoTime();
  14. }
  15. void end() {
  16. time = System.nanoTime() - start;
  17. }
  18. String prettyTime() {
  19. if (time < 1000)
  20. return time+"ns";
  21. else if (time < 1000000)
  22. return (time / 1000)+"μs";
  23. else if (time < 1000000000)
  24. return (time / 1000000)+"ms";
  25. else
  26. return (time / 1000000000)+"s";
  27. }
  28. String prettySpeedup(Timer base) {
  29. double speedup = (double)base.time / (double)time;
  30. return String.format("%s (%.2fx speedup)",
  31. prettyTime(), speedup);
  32. }
  33. }
  34. static class TestResult {
  35. Timer baseTime = null;
  36. Timer seqTime = null;
  37. Timer parTime = null;
  38. int n = 0;
  39. int k = 0;
  40. // Print a test result
  41. void print() {
  42. String tmpl =
  43. "\nTest results for n=%d, k=%d:\n"+
  44. "\tArrays.sort: %s\n"+
  45. "\tSequential: %s\n"+
  46. "\tParallel: %s\n";
  47. System.out.printf(tmpl,
  48. n, k, baseTime.prettyTime(),
  49. seqTime.prettySpeedup(baseTime),
  50. parTime.prettySpeedup(baseTime));
  51. }
  52. // Create a TestResult which is the average
  53. // of n other TestResults
  54. static TestResult avg(TestResult[] results) {
  55. TestResult ts = new TestResult();
  56. ts.baseTime = new Timer();
  57. ts.seqTime = new Timer();
  58. ts.parTime = new Timer();
  59. ts.n = results[0].n;
  60. ts.k = results[0].k;
  61. for (TestResult t: results) {
  62. if (t.n != ts.n)
  63. throw new RuntimeException("Bad n value");
  64. if (t.k != ts.k)
  65. throw new RuntimeException("Bad k value");
  66. ts.baseTime.time += t.baseTime.time;
  67. ts.seqTime.time += t.seqTime.time;
  68. ts.parTime.time += t.parTime.time;
  69. }
  70. ts.baseTime.time /= results.length;
  71. ts.seqTime.time /= results.length;
  72. ts.parTime.time /= results.length;
  73. return ts;
  74. }
  75. }
  76. static TestResult runTest(int[] a, int n, int k, Timer baseTime) {
  77. TestResult ts = new TestResult();
  78. Timer seqTime = new Timer();
  79. Timer parTime = new Timer();
  80. A2Solver seq = new Sequential();
  81. A2Solver par = new Parallel();
  82. int[] copy = new int[n];
  83. // Test seq
  84. System.arraycopy(a, 0, copy, 0, n);
  85. seqTime.start();
  86. seq.solveA2(copy, k);
  87. seqTime.end();
  88. // Test par
  89. System.arraycopy(a, 0, copy, 0, n);
  90. parTime.start();
  91. par.solveA2(copy, k);
  92. parTime.end();
  93. ts.baseTime = baseTime;
  94. ts.seqTime = seqTime;
  95. ts.parTime = parTime;
  96. ts.n = n;
  97. ts.k = k;
  98. return ts;
  99. }
  100. static void runTests() {
  101. int[] ns = { 100000000, 10000000, 1000000, 100000, 10000, 1000 };
  102. int[] ks = { 100, 20 };
  103. int iters = 7;
  104. for (int n: ns) {
  105. Timer baseTime = new Timer();
  106. // Construct array
  107. Random rand = new Random(randSeed);
  108. int[] a = new int[n];
  109. for (int i = 0; i < n; ++i) {
  110. a[i] = rand.nextInt();
  111. }
  112. // Time Arrays.sort()
  113. // I only do this once per n because it takes a long time.
  114. int[] copy = new int[n];
  115. System.arraycopy(a, 0, copy, 0, n);
  116. baseTime.start();
  117. Arrays.sort(copy);
  118. baseTime.end();
  119. for (int k: ks) {
  120. TestResult[] results = new TestResult[iters];
  121. for (int i = 0; i < iters; ++i) {
  122. results[i] = runTest(a, n, k, baseTime);
  123. }
  124. TestResult t = TestResult.avg(results);
  125. t.print();
  126. }
  127. }
  128. }
  129. public static void main(String[] args) {
  130. if (args.length < 1) {
  131. usage();
  132. System.exit(1);
  133. }
  134. if (args[0].equals("test")) {
  135. runTests();
  136. return;
  137. }
  138. A2Solver solver;
  139. int n, k;
  140. try {
  141. n = Integer.parseInt(args[0]);
  142. k = Integer.parseInt(args[1]);
  143. if (args.length == 3) {
  144. if (args[2].equals("seq"))
  145. solver = new Sequential();
  146. else if (args[2].equals("par"))
  147. solver = new Parallel();
  148. else {
  149. usage();
  150. System.exit(1);
  151. return;
  152. }
  153. } else {
  154. solver = new Sequential();
  155. }
  156. } catch (Exception ex) {
  157. System.out.println(ex.getMessage());
  158. System.out.println("");
  159. usage();
  160. System.exit(1);
  161. return;
  162. }
  163. Timer timer = new Timer();
  164. // Create array
  165. int[] a = new int[n];
  166. Random r = new Random(randSeed);
  167. for (int i = 0; i < n; ++i) {
  168. a[i] = r.nextInt();
  169. }
  170. // Solve
  171. timer.start();
  172. solver.solveA2(a, k);
  173. timer.end();
  174. // Print
  175. for (int i = 0; i < k; ++i) {
  176. if (i != 0)
  177. System.out.print(" ");
  178. System.out.print(a[i]);
  179. }
  180. System.out.println("");
  181. // Print report info to stderr, so that
  182. // stdout is only the result
  183. System.err.printf("\nn=%d, k=%d, %s time: %s\n",
  184. n, k, solver.getClass().getName(), timer.prettyTime());
  185. }
  186. }