Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.logging.Level;
- import net.tuis.ubench.UBench;
- import net.tuis.ubench.UUtils;
- public class KnightMove {
- @FunctionalInterface
- public interface KValid {
- boolean isValid(int r1, int c1, int r2, int c2);
- }
- public static boolean isValidAbs(int r1, int c1, int r2, int c2) {
- int row = Math.abs(r2 - r1);
- int col = Math.abs(c2 - c1);
- return ((row == 2 && col == 1) || (row == 1 && col == 2));
- }
- public static boolean isValidPyth(int r1, int c1, int r2, int c2) {
- // use pythagoras to ensure that a move makes a right-angled
- // triangle move with sides of 1 and 2. 1-squared + 2 squared is 5.
- int deltaR = r2 - r1;
- int deltaC = c2 - c1;
- return 5 == deltaR * deltaR + deltaC * deltaC;
- }
- public static int countValid(final KValid ivalid, final int size) {
- int count = 0;
- for (int r1 = 0; r1 < size; r1++) {
- for (int c1 = 0; c1 < size; c1++) {
- for (int r2 = 0; r2 < size; r2++) {
- for (int c2 = 0; c2 < size; c2++) {
- if (ivalid.isValid(r1, c1, r2, c2)) {
- count++;
- }
- }
- }
- }
- }
- return count;
- }
- public static void main(String[] args) {
- UUtils.setStandaloneLogging(Level.INFO);
- UBench bench = new UBench("Knights");
- bench.addTask("Pyth", () -> countValid(KnightMove::isValidPyth, 20));
- bench.addTask("Absc", () -> countValid(KnightMove::isValidAbs, 20));
- bench.press(1000).report();
- bench.press(1000).report();
- }
- /*
- INFO 2015-09-26 22:28:42.670 net.tuis.ubench.UBench(<init>): Creating UBench for suite Knights
- 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
- Task Knights -> Pyth: (Unit: MILLISECONDS)
- Count : 1000 Average : 0.1448
- Fastest : 0.1160 Slowest : 8.0058
- 95Pctile : 0.1895 99Pctile : 0.2396
- TimeBlock : 0.304 0.133 0.132 0.132 0.128 0.125 0.124 0.124 0.124 0.122
- Histogram : 984 13 1 0 0 1 1
- Task Knights -> Absc: (Unit: MILLISECONDS)
- Count : 1000 Average : 0.2256
- Fastest : 0.1887 Slowest : 11.3645
- 95Pctile : 0.2842 99Pctile : 0.3951
- TimeBlock : 0.366 0.216 0.215 0.213 0.212 0.209 0.212 0.205 0.204 0.204
- Histogram : 990 9 0 0 0 1
- 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
- Task Knights -> Pyth: (Unit: MILLISECONDS)
- Count : 1000 Average : 0.1272
- Fastest : 0.1157 Slowest : 0.4646
- 95Pctile : 0.1571 99Pctile : 0.2432
- TimeBlock : 0.124 0.125 0.151 0.131 0.121 0.125 0.123 0.123 0.122 0.125
- Histogram : 983 16 1
- Task Knights -> Absc: (Unit: MILLISECONDS)
- Count : 1000 Average : 0.2080
- Fastest : 0.1883 Slowest : 0.4350
- 95Pctile : 0.2562 99Pctile : 0.3734
- TimeBlock : 0.204 0.202 0.238 0.216 0.202 0.202 0.207 0.202 0.200 0.205
- Histogram : 993 7
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement