Advertisement
Guest User

Performance for two different knight moves

a guest
Sep 26th, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. import java.util.logging.Level;
  2.  
  3. import net.tuis.ubench.UBench;
  4. import net.tuis.ubench.UUtils;
  5.  
  6.  
  7. public class KnightMove {
  8.    
  9.     @FunctionalInterface
  10.     public interface KValid {
  11.         boolean isValid(int r1, int c1, int r2, int c2);
  12.     }
  13.    
  14.     public static boolean isValidAbs(int r1, int c1, int r2, int c2) {
  15.         int row = Math.abs(r2 - r1);
  16.         int col = Math.abs(c2 - c1);
  17.         return ((row == 2 && col == 1) || (row == 1 && col == 2));        
  18.     }
  19.    
  20.     public static boolean isValidPyth(int r1, int c1, int r2, int c2) {
  21.         // use pythagoras to ensure that a move makes a right-angled
  22.         // triangle move with sides of 1 and 2. 1-squared + 2 squared is 5.
  23.         int deltaR = r2 - r1;
  24.         int deltaC = c2 - c1;
  25.         return 5 == deltaR * deltaR + deltaC * deltaC;
  26.     }
  27.    
  28.     public static int countValid(final KValid ivalid, final int size) {
  29.         int count = 0;
  30.         for (int r1 = 0; r1 < size; r1++) {
  31.             for (int c1 = 0; c1 < size; c1++) {
  32.                 for (int r2 = 0; r2 < size; r2++) {
  33.                     for (int c2 = 0; c2 < size; c2++) {
  34.                         if (ivalid.isValid(r1, c1, r2, c2)) {
  35.                             count++;
  36.                         }
  37.                     }
  38.                 }
  39.                
  40.             }
  41.         }
  42.         return count;
  43.     }
  44.    
  45.     public static void main(String[] args) {
  46.         UUtils.setStandaloneLogging(Level.INFO);
  47.         UBench bench = new UBench("Knights");
  48.         bench.addTask("Pyth", () -> countValid(KnightMove::isValidPyth, 20));
  49.         bench.addTask("Absc", () -> countValid(KnightMove::isValidAbs, 20));
  50.         bench.press(1000).report();
  51.         bench.press(1000).report();
  52.     }
  53.    
  54.     /*
  55.  
  56.     INFO   2015-09-26 22:28:42.670 net.tuis.ubench.UBench(<init>): Creating UBench for suite Knights
  57.     INFO   2015-09-26 22:28:43.077 net.tuis.ubench.UBench(press): UBench suite Knights: completed benchmarking all tasks using mode INTERLEAVED in 388.177ms
  58.     Task Knights -> Pyth: (Unit: MILLISECONDS)
  59.       Count    :     1000      Average  :   0.1448
  60.       Fastest  :   0.1160      Slowest  :   8.0058
  61.       95Pctile :   0.1895      99Pctile :   0.2396
  62.       TimeBlock : 0.304 0.133 0.132 0.132 0.128 0.125 0.124 0.124 0.124 0.122
  63.       Histogram :   984    13     1     0     0     1     1
  64.  
  65.     Task Knights -> Absc: (Unit: MILLISECONDS)
  66.       Count    :     1000      Average  :   0.2256
  67.       Fastest  :   0.1887      Slowest  :  11.3645
  68.       95Pctile :   0.2842      99Pctile :   0.3951
  69.       TimeBlock : 0.366 0.216 0.215 0.213 0.212 0.209 0.212 0.205 0.204 0.204
  70.       Histogram :   990     9     0     0     0     1
  71.  
  72.     INFO   2015-09-26 22:28:43.436 net.tuis.ubench.UBench(press): UBench suite Knights: completed benchmarking all tasks using mode INTERLEAVED in 337.284ms
  73.     Task Knights -> Pyth: (Unit: MILLISECONDS)
  74.       Count    :     1000      Average  :   0.1272
  75.       Fastest  :   0.1157      Slowest  :   0.4646
  76.       95Pctile :   0.1571      99Pctile :   0.2432
  77.       TimeBlock : 0.124 0.125 0.151 0.131 0.121 0.125 0.123 0.123 0.122 0.125
  78.       Histogram :   983    16     1
  79.  
  80.     Task Knights -> Absc: (Unit: MILLISECONDS)
  81.       Count    :     1000      Average  :   0.2080
  82.       Fastest  :   0.1883      Slowest  :   0.4350
  83.       95Pctile :   0.2562      99Pctile :   0.3734
  84.       TimeBlock : 0.204 0.202 0.238 0.216 0.202 0.202 0.207 0.202 0.200 0.205
  85.       Histogram :   993     7
  86.  
  87.       */
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement