code_junkie

comparison of code performance, threaded versus non-threaded

Nov 12th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.26 KB | None | 0 0
  1. //code without thread use
  2. public static int getNextPrime(int from) {
  3. int nextPrime = from+1;
  4. boolean superPrime = false;
  5. while(!superPrime) {
  6. boolean prime = true;
  7. for(int i = 2;i &lt nextPrime;i++) {
  8. if(nextPrime % i == 0) {
  9. prime = false;
  10. }
  11. }
  12. if(prime) {
  13. superPrime = true;
  14. } else {
  15. nextPrime++;
  16. }
  17. }
  18. return nextPrime;
  19. }
  20.  
  21. public static void main(String[] args) {
  22. int primeStart = 5;
  23. ArrayList list = new ArrayList();
  24. for(int i = 0;i &lt 10000;i++) {
  25. list.add(primeStart);
  26. primeStart = getNextPrime(primeStart);
  27. }
  28. }
  29.  
  30. public class PrimeRunnable implements Runnable {
  31.  
  32. private int from;
  33. private int lastPrime;
  34.  
  35. public PrimeRunnable(int from) {
  36. this.from = from;
  37. }
  38.  
  39. public boolean isPrime(int number) {
  40. for(int i = 2;i &lt from;i++) {
  41. if((number % i) == 0) {
  42. return false;
  43. }
  44. }
  45. lastPrime = number;
  46. return true;
  47. }
  48.  
  49. public int getLastPrime() {
  50. return lastPrime;
  51. }
  52.  
  53. public void run() {
  54. while(!isPrime(++from))
  55. ;
  56. }
  57. }
  58.  
  59. public static void main(String[] args) {
  60. int primeStart = 5;
  61. ArrayList list = new ArrayList();
  62. for(int i = 0;i &lt 10000;i++) {
  63. PrimeRunnable pr = new PrimeRunnable(primeStart);
  64. Thread t = new Thread(pr);
  65. t.start();
  66. t.join();
  67. primeStart = pr.getLastPrime();
  68. list.add(primeStart);
  69. }
  70. }
  71.  
  72. public static void main(String[] args) {
  73. int primeStart = 5;
  74.  
  75. // Make thread-safe list for adding results to
  76. List list = Collections.synchronizedList(new ArrayList());
  77.  
  78. // Pull thread pool count out into a value so you can easily change it
  79. int threadCount = 10000;
  80. Thread[] threads = new Thread[threadCount];
  81.  
  82. // Start all threads
  83. for(int i = 0;i < threadCount;i++) {
  84. // Pass list to each Runnable here
  85. // Also, I added +i here as I think the intention is
  86. // to test 10000 possible numbers>5 for primeness -
  87. // was testing 5 in all loops
  88. PrimeRunnable pr = new PrimeRunnable(primeStart+i, list);
  89. Thread[i] threads = new Thread(pr);
  90. threads[i].start(); // thread is now running in parallel
  91. }
  92.  
  93. // All threads now running in parallel
  94.  
  95. // Then wait for all threads to complete
  96. for(int i=0; i<threadCount; i++) {
  97. threads[i].join();
  98. }
  99. }
  100.  
  101. public class PrimeRunnable implements Runnable {
  102. private int from;
  103. private List results; // shared but thread-safe
  104.  
  105. public PrimeRunnable(int from, List results) {
  106. this.from = from;
  107. this.results = results;
  108. }
  109.  
  110. public void isPrime(int number) {
  111. for(int i = 2;i < from;i++) {
  112. if((number % i) == 0) {
  113. return;
  114. }
  115. }
  116. // found prime, add to shared results
  117. this.results.add(number);
  118. }
  119.  
  120. public void run() {
  121. isPrime(from); // don't increment, just check one number
  122. }
  123. }
  124.  
  125. public static void main(String[] args) {
  126. int primeStart = 5;
  127.  
  128. // Make thread-safe list for adding results to
  129. List list = Collections.synchronizedList(new ArrayList());
  130.  
  131. int threadCount = 16; // Experiment with this to find best on your machine
  132. ExecutorService exec = Executors.newFixedThreadPool(threadCount);
  133.  
  134. int workCount = 10000; // See how # of work is now separate from # of threads?
  135. for(int i = 0;i < workCount;i++) {
  136. // submit work to the svc for execution across the thread pool
  137. exec.execute(new PrimeRunnable(primeStart+i, list));
  138. }
  139.  
  140. // Wait for all tasks to be done or timeout to go off
  141. exec.awaitTermination(1, TimeUnit.DAYS);
  142. }
  143.  
  144. private static int currentPrime;
  145. public static void main(String[] args) throws InterruptedException {
  146. for (currentPrime = 0; currentPrime < 10000; currentPrime++) {
  147. Thread t = new Thread(new Runnable() {
  148. public void run() {
  149. getNextPrime(currentPrime);
  150. }});
  151. t.run();
  152. t.join();
  153. }
  154. }
  155.  
  156. for(int i = 2;i < nextPrime;i++) {
  157. if(nextPrime % i == 0) {
  158. prime = false;
  159. }
  160. }
  161.  
  162. for(int i = 2;i < from;i++) {
  163. if((number % i) == 0) {
  164. return false;
  165. }
  166. }
  167.  
  168. for(int i = 2;i < nextPrime;i++) {
  169. if(nextPrime % i == 0) {
  170. prime = false;
  171. break;
  172. }
  173. }
Add Comment
Please, Sign In to add comment