Advertisement
Guest User

Updated: Threaded Array Read test

a guest
Feb 15th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. import java.util.concurrent.Executors;
  2. import java.util.concurrent.ThreadPoolExecutor;
  3. import java.util.concurrent.TimeUnit;
  4.  
  5. import javax.swing.SwingWorker;
  6.  
  7.  
  8.  
  9.  
  10. public class TestField {
  11.    
  12.    
  13.     public static void main(String[] args) {
  14.        
  15.         //Storing stuff, obvious stuff
  16.         int numIterations = 5000;
  17.         double singleTimeSum = 0, multiTimeSum = 0;
  18.        
  19.         //hasPrint and anything to do with it are simply a convenience thing, for print out the percentage complete every 10%
  20.         boolean hasPrint = true;
  21.        
  22.         //Initialise thread pool size 2
  23.         pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(2);
  24.        
  25.         for(int i=0; i<numIterations; i++) {
  26.             //Again, these two lines are just convenience, they print out x% every 10 percent
  27.             if(!hasPrint && ((i*100)/numIterations) % 10 == 0) System.out.println((i*100)/numIterations + "%");
  28.             hasPrint = ((i*100)/numIterations) % 10 == 0;
  29.  
  30.             //Run test
  31.             new Go();
  32.            
  33.            
  34.             try {
  35.                 //We gotta hold up while our threads properly die, otherwise it makes them faster than it can destroy them,
  36.                 //and computer is SAD
  37.                 Thread.sleep(10);
  38.             } catch (InterruptedException e) {
  39.                 e.printStackTrace();
  40.             }
  41.            
  42.             //Add time to sum
  43.             singleTimeSum += singleTime;
  44.             multiTimeSum += multiTime;
  45.            
  46.             //and reset times
  47.             singleTime = 0;
  48.             multiTime = 0;
  49.         }
  50.        
  51.         //Print out results
  52.         System.out.println("Results (across " + numIterations + " tests): ");
  53.         System.out.println("Single-Thread time:\t" + (singleTimeSum / (float)numIterations));
  54.         System.out.println("Multi-Thread time:\t" + (multiTimeSum / (float)numIterations));
  55.     }
  56.  
  57.    
  58.     //Go references these variables, as though "out" parameters from a class.
  59.     static ThreadPoolExecutor pool;
  60.     static double multiTime, singleTime;
  61.    
  62.     //Runs the test once, stores results in multiTime, singleTime
  63.     static class Go {
  64.         public Go() {
  65.            
  66.             //Fill an array of 20k random doubles, just, because.
  67.             final double[] array = new double[20000];
  68.             for (int i = 0; i < array.length; i++) array[i] = Math.random();
  69.            
  70.             //The two threads store their results in here
  71.             final double[] resultA, resultB;
  72.             resultA = new double[1];
  73.             resultB = new double[1];
  74.            
  75.             //This is the worker thread
  76.             SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
  77.  
  78.                 @Override
  79.                 protected Void doInBackground() throws Exception {
  80.                     double temp;
  81.                    
  82.                     //For timing, start clock
  83.                     long localTime = System.nanoTime();
  84.                    
  85.                     //read from the array start-to-end, 6000 times
  86.                     for(int readIndex = 0; readIndex < 6000; readIndex++) {
  87.                         for (int i = 0; i < array.length; i++) {
  88.                             temp = array[i];
  89.                         }
  90.                     }
  91.                    
  92.                     //Store end time now, so that we may perform processign with this value without overhead
  93.                     long endTime = System.nanoTime();
  94.                    
  95.                     //Store in our array, the result
  96.                     resultA[0] = (endTime - localTime)/1e6;
  97.                     return null;
  98.                 }
  99.                
  100.             };
  101.  
  102.             //This is an exact copy of the worker thread, I was unsure if just executing the SAME instance of it would execute as expected
  103.             SwingWorker<Void, Void> worker2 = new SwingWorker<Void, Void>() {
  104.                
  105.                 @Override
  106.                 protected Void doInBackground() throws Exception {
  107.                     double temp;
  108.                    
  109.                     long localTime = System.nanoTime();
  110.                    
  111.                     for(int readIndex = 0; readIndex < 6000; readIndex++) {
  112.                         for (int i = 0; i < array.length; i++) {
  113.                             temp = array[i];
  114.                         }
  115.                     }
  116.                    
  117.                     long endTime = System.nanoTime();
  118.                    
  119.                     resultB[0] = (endTime - localTime)/1e6;
  120.                     return null;
  121.                 }
  122.                
  123.             };
  124.            
  125.            
  126.             //Thread pool
  127.             ThreadPoolExecutor pool = (ThreadPoolExecutor) Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
  128.            
  129.             //Execute these tasks on our static ThreadPoolExecutor instance
  130.             pool.execute(worker);
  131.             pool.execute(worker2);
  132.            
  133.             // We do not need to shutdown the thread pool
  134.            
  135.             //Average the two Thread times.
  136.             multiTime += (resultA[0] + resultB[0]) / 2.0;
  137.            
  138.             //Basically, run the same code on the main thread.
  139.             //Could have made another worker to just run by itself, but, meh
  140.             double temp;
  141.             long startTime = System.nanoTime();
  142.             for(int r=0; r<6000; r++) {
  143.                 for (int i = 0; i < array.length; i++) {
  144.                     temp = array[i];
  145.                 }
  146.             }
  147.            
  148.             //Store single time
  149.             singleTime += (System.nanoTime() - startTime)/1e6;
  150.         }
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement