Advertisement
Guest User

Fixed Benchmark Code

a guest
Feb 15th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.20 KB | None | 0 0
  1. import java.math.BigDecimal;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.Iterator;
  5. import java.util.concurrent.ConcurrentLinkedQueue;
  6. import java.util.concurrent.CountDownLatch;
  7.  
  8. public class Primer2 {
  9.     public Integer stall = 1;
  10.  
  11.     private ConcurrentLinkedQueue<Integer> numbs;
  12.     public long start;
  13.  
  14.     public Primer2() {
  15.  
  16.         numbs = new ConcurrentLinkedQueue<Integer>();
  17.     }
  18.  
  19.  
  20.     // ///////////////////////////////////////////////////////
  21.     class Task implements Runnable {
  22.  
  23.         private int[] numbs;
  24.         //private ArrayList<Integer> numbs;
  25.         private int myIndex;
  26.        
  27.         private CountDownLatch lat;
  28.         private Runtime runtime;
  29.         //private ArrayList<Integer> al;
  30.         private int[] al;
  31.         private final int s = 32343;
  32.  
  33.         private Task() {
  34.  
  35.         }
  36.  
  37.         public Task(ConcurrentLinkedQueue<Integer> numbs, CountDownLatch lat,
  38.                         Runtime runtime) {
  39.             Integer[] temp = new Integer[0];
  40.             temp = numbs.toArray(temp);
  41.             this.numbs = new int[temp.length];
  42.             for (int index = 0; index < temp.length; index++) {
  43.                 this.numbs[index] = temp[index];
  44.             }
  45.             //this.numbs = new ArrayList<Integer>(numbs);
  46.            
  47.             this.myIndex = 0;
  48.             this.lat = lat;
  49.             this.runtime = runtime;
  50.             this.al = new int[s];
  51.             //this.al = new ArrayList<Integer>(s);
  52.         }
  53.        
  54.         public boolean isPrime (int num) {         
  55.             //ArrayList<Integer> al = new ArrayList<Integer>(s);
  56.             //al.clear();
  57.             for (int c = 0; c < s; c++) {
  58.                 //al.add(c);
  59.                 al[c] = c;
  60.             }
  61.             //Iterator<Integer> it = al.iterator();
  62.             //if (it.hasNext()) {
  63.             //  int c = it.next();
  64.             //  c = c++;
  65.             //}
  66.             for (int c : al) {
  67.                 c++;
  68.             }
  69.  
  70.             boolean rv = true;
  71.             if (num > 2) {
  72.  
  73.                 for (int d = 2; d < num; d++) {
  74.                     if (num % d == 0) {
  75.                         rv = false;
  76.                         break;
  77.                     }
  78.                 }
  79.             } else if (num < 2) {
  80.                 rv = false;
  81.             }
  82.             return rv;
  83.         }
  84.  
  85.         public void run () {
  86.  
  87.             //stall++;
  88.             long myStart = System.nanoTime();
  89.             //int totalNum = numbs.size();
  90.             int totalNum = numbs.length;
  91.             while ( myIndex < totalNum) {
  92.                 //isPrime(numbs.get(myIndex));
  93.                 isPrime(numbs[myIndex]);
  94.                 myIndex++;
  95.             }
  96.             long end = System.nanoTime();
  97.             runtime.setRuntime( (end - myStart) / 10e5);
  98.             lat.countDown();
  99.         }
  100.  
  101.     }
  102.  
  103.     class Runtime {
  104.  
  105.         private double runtime;
  106.  
  107.         public double getRuntime () {
  108.  
  109.             return runtime;
  110.         }
  111.  
  112.         public void setRuntime (double runtime) {
  113.  
  114.             this.runtime = runtime;
  115.         }
  116.  
  117.     }
  118.  
  119.     // /////////////////////////////////////////////////
  120.     public static void main(String[] args) {
  121.  
  122.         System.out.println("------------------start------------------");
  123.         int k = 1;
  124.         if (args.length > 0) {
  125.             k = Integer.valueOf(args[0]);
  126.         }
  127.         CountDownLatch lat = new CountDownLatch(k);
  128.         Primer2 p = new Primer2();
  129.         for (int i = 0; i < 100000; i++) {
  130.             p.numbs.add(i);
  131.         }
  132.  
  133.         ConcurrentLinkedQueue<Integer>[] qjobs = (ConcurrentLinkedQueue<Integer>[]) new ConcurrentLinkedQueue[k];
  134.         for (int c = 0; c < k; c++) {
  135.             qjobs[c] = new ConcurrentLinkedQueue<Integer>();
  136.         }
  137.  
  138.         int i = 0;
  139.         for (int n : p.numbs) {
  140.             qjobs[i++ % k].add(n);
  141.         }
  142.  
  143.         Thread[] ts = new Thread[k];
  144.         Runtime[] runtimes = new Runtime[k];
  145.         for (int c = 0; c < k; c++) {
  146.             runtimes[c] = p.new Runtime();
  147.             ts[c] = new Thread(p.new Task(qjobs[c], lat, runtimes[c]));
  148.         }
  149.         p.start = System.nanoTime();
  150.         for (int c = 0; c < k; c++) {
  151.             ts[c].start();
  152.         }
  153.         try {
  154.             lat.await();
  155.             long end = System.nanoTime();
  156.             for (int c = 0; c < k; c++) {
  157.                 ts[c].interrupt();
  158.             }
  159.             System.out.println(k + " threads' runtimes:");
  160.             double[] rt = new double[k];
  161.             for (int c = 0; c < k; c++) {
  162.                 rt[c] = (new BigDecimal(runtimes[c].getRuntime()).setScale(0,
  163.                                 BigDecimal.ROUND_HALF_UP)).doubleValue();
  164.                 System.out.println( (c + 1) + "\t\t" + rt[c]);
  165.             }
  166.  
  167.             Arrays.sort(rt);
  168.             System.out.println("maximum:\t" + rt[k - 1]);
  169.             System.out.println("main time:\t"
  170.                             + (new BigDecimal( (end - p.start) / 10e5).setScale(
  171.                                             0, BigDecimal.ROUND_HALF_UP))
  172.                                             .doubleValue());
  173.             System.out.println("------------------end------------------");
  174.         } catch (Exception e) {
  175.             e.printStackTrace();
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement