Advertisement
Guest User

Java Test Point Distance Time

a guest
May 9th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.78 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.util.ArrayList;
  3.  
  4.  
  5. public class TestDistance {
  6.    
  7.     /**
  8.      * Testing the average time to get the distance to the closest point.
  9.      *
  10.      * I am using average because sometimes java will run slow and sometimes run fast...
  11.      * Running a lot of results and getting the average seems to be the most reliable method.
  12.      *
  13.      * @param args
  14.      */
  15.     public static void main(String[] args){
  16.         long start, end;
  17.         Point p = new Point(0,0);
  18.         ArrayList<Point> points = new ArrayList<Point>();
  19.         ArrayList<Long> results = new ArrayList<Long>();
  20.         ArrayList<Double> results2 = new ArrayList<Double>();
  21.         int pstart = 10;
  22.        
  23.         for(int w = 0; w < 3; w++){    
  24.             //Assign points.
  25.             for(int a = 0; a < pstart; a++)
  26.                 for(int b = 0; b < pstart; b++)
  27.                     points.add(new Point(a,b));
  28.            
  29.             //Loop over to get average
  30.             for(int z = 0; z < 10; z++){
  31.                
  32.                 //Loop 100 times to get a average
  33.                 for(int i = 0; i < 100; i++){
  34.                     start = System.currentTimeMillis();
  35.                    
  36.                     getClosest(p,points);
  37.                    
  38.                     end = System.currentTimeMillis() - start;
  39.                     results.add(end);
  40.                     //System.out.println("Time taken: " + end + " ms.");
  41.                 }
  42.                
  43.                 results2.add(calculateAverageLong(results));
  44.                 //System.out.println("Average time (Out of 100): " + calculateAverage(results) + "ms to find the closest point out of " + points.size() + " points.");
  45.                 results.clear();
  46.             }
  47.            
  48.             System.out.println("Average time TOTAL (10 loops of 100 loops): " + calculateAverageDouble(results2) + "ms to find the closest point out of " + points.size() + " points.");
  49.             results2.clear();
  50.             points.clear();
  51.            
  52.             //Increase points to check
  53.             switch(pstart){
  54.             case 100:
  55.                 pstart = 1000;
  56.                 break;
  57.             case 10:
  58.                 pstart = 100;
  59.                 break;
  60.             }
  61.         }
  62.     }
  63.    
  64.     /**
  65.      * Calculate the average for longs.
  66.      *
  67.      * @param marks
  68.      * @return Long
  69.      */
  70.     private static double calculateAverageLong(ArrayList<Long> marks) {
  71.         Long sum = 0L;
  72.        
  73.         for(Long mark : marks)
  74.             sum += mark;
  75.        
  76.         return (sum.doubleValue() / marks.size());
  77.     }
  78.    
  79.     /**
  80.      * Calculate the average for doubles.
  81.      *
  82.      * @param marks
  83.      * @return Double
  84.      */
  85.     private static double calculateAverageDouble(ArrayList<Double> marks) {
  86.         Double sum = 0D;
  87.        
  88.         for(Double mark : marks)
  89.             sum += mark;
  90.        
  91.         return sum / marks.size();
  92.     }
  93.    
  94.     /**
  95.      * Get the closest point.
  96.      *
  97.      * @param start
  98.      * @param points
  99.      * @return Point
  100.      */
  101.     public static Point getClosest(Point start, ArrayList<Point> points) {
  102.         Point closest = null;
  103.         double hold = 0;
  104.         int nearest = 999999;
  105.  
  106.         for(Point p : points){
  107.            
  108.             if(p != null){
  109.                 hold = p.distance(start);
  110.                 if(hold < nearest){
  111.                     nearest = (int) p.distance(start);
  112.                     closest = p;
  113.                 }
  114.             }
  115.         }
  116.        
  117.         return closest;
  118.     }
  119.  
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement