Home | History | Annotate | Download | only in src
      1 import otherpackage.OtherPackagePublicEnum;
      2 
      3 public class Main {
      4     /** used by {@link #basisCall} */
      5     static private int basisTestValue = 12;
      6 
      7     static public void main(String[] args) throws Exception {
      8         boolean timing = (args.length >= 1) && args[0].equals("--timing");
      9         run(timing);
     10     }
     11 
     12     static public void run(boolean timing) {
     13         preTest();
     14 
     15         long time0 = System.nanoTime();
     16         int count1 = test1(500);
     17         long time1 = System.nanoTime();
     18         int count2 = test2(500);
     19         long time2 = System.nanoTime();
     20         int count3 = test3(500);
     21         long time3 = System.nanoTime();
     22         int count4 = basis(500);
     23         long time4 = System.nanoTime();
     24 
     25         System.out.println("basis: performed " + count4 + " iterations");
     26         System.out.println("test1: performed " + count1 + " iterations");
     27         System.out.println("test2: performed " + count2 + " iterations");
     28         System.out.println("test3: performed " + count3 + " iterations");
     29 
     30         double msec1 = (time1 - time0) / (double) count1 / 1000000;
     31         double msec2 = (time2 - time1) / (double) count2 / 1000000;
     32         double msec3 = (time3 - time2) / (double) count3 / 1000000;
     33         double basisMsec = (time4 - time3) / (double) count4 / 1000000;
     34 
     35         double avg = (msec1 + msec2 + msec3) / 3;
     36         if (avg < (basisMsec * 10)) {
     37             System.out.println("Timing is acceptable.");
     38         } else {
     39             System.out.println("Iterations are taking too long!");
     40             timing = true;
     41         }
     42 
     43         if (timing) {
     44             System.out.printf("basis time: %.3g msec\n", basisMsec);
     45             System.out.printf("test1: %.3g msec per iteration\n", msec1);
     46             System.out.printf("test2: %.3g msec per iteration\n", msec2);
     47             System.out.printf("test3: %.3g msec per iteration\n", msec3);
     48         }
     49 
     50     }
     51 
     52     static public void preTest() {
     53         /*
     54          * This is meant to ensure that the basic enum functionality
     55          * really is working.
     56          */
     57 
     58         Class<SamePackagePublicEnum> c = SamePackagePublicEnum.class;
     59 
     60         System.out.println(Enum.valueOf(c, "FOUR"));
     61         System.out.println(Enum.valueOf(c, "ONE"));
     62         System.out.println(Enum.valueOf(c, "FOURTEEN"));
     63         System.out.println(Enum.valueOf(c, "NINE"));
     64         System.out.println(Enum.valueOf(c, "FIVE"));
     65         System.out.println(Enum.valueOf(c, "TWELVE"));
     66 
     67         System.out.println(Enum.valueOf(c, "ZERO").getClass().getName());
     68     }
     69 
     70     static final String[] BASIS_COMPARE_ARRAY = {
     71         "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT",
     72         "NINE", "TEN", "ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN",
     73         "SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"
     74     };
     75 
     76     static public int basis(int iters) {
     77         for (int i = iters; i > 0; i--) {
     78             basisValueOf("ZERO");
     79             basisValueOf("ONE");
     80             basisValueOf("TWO");
     81             basisValueOf("THREE");
     82             basisValueOf("FOUR");
     83             basisValueOf("FIVE");
     84             basisValueOf("SIX");
     85             basisValueOf("SEVEN");
     86             basisValueOf("EIGHT");
     87             basisValueOf("NINE");
     88             basisValueOf("TEN");
     89             basisValueOf("ELEVEN");
     90             basisValueOf("TWELVE");
     91             basisValueOf("THIRTEEN");
     92             basisValueOf("FOURTEEN");
     93             basisValueOf("FIFTEEN");
     94             basisValueOf("SIXTEEN");
     95             basisValueOf("SEVENTEEN");
     96             basisValueOf("EIGHTEEN");
     97             basisValueOf("NINETEEN");
     98         }
     99 
    100         return iters * 20;
    101     }
    102 
    103     static String basisValueOf(String key) {
    104         for (String s : BASIS_COMPARE_ARRAY) {
    105             if (s.equals(key)) {
    106                 return s;
    107             }
    108         }
    109         throw new IllegalArgumentException();
    110     }
    111 
    112     static public int test1(int iters) {
    113         Class<SamePackagePublicEnum> c = SamePackagePublicEnum.class;
    114         for (int i = iters; i > 0; i--) {
    115             Enum.valueOf(c, "ZERO");
    116             Enum.valueOf(c, "ONE");
    117             Enum.valueOf(c, "TWO");
    118             Enum.valueOf(c, "THREE");
    119             Enum.valueOf(c, "FOUR");
    120             Enum.valueOf(c, "FIVE");
    121             Enum.valueOf(c, "SIX");
    122             Enum.valueOf(c, "SEVEN");
    123             Enum.valueOf(c, "EIGHT");
    124             Enum.valueOf(c, "NINE");
    125             Enum.valueOf(c, "TEN");
    126             Enum.valueOf(c, "ELEVEN");
    127             Enum.valueOf(c, "TWELVE");
    128             Enum.valueOf(c, "THIRTEEN");
    129             Enum.valueOf(c, "FOURTEEN");
    130             Enum.valueOf(c, "FIFTEEN");
    131             Enum.valueOf(c, "SIXTEEN");
    132             Enum.valueOf(c, "SEVENTEEN");
    133             Enum.valueOf(c, "EIGHTEEN");
    134             Enum.valueOf(c, "NINETEEN");
    135         }
    136 
    137         return iters * 20;
    138     }
    139 
    140     static public int test2(int iters) {
    141         Class<SamePackagePrivateEnum> c = SamePackagePrivateEnum.class;
    142         for (int i = iters; i > 0; i--) {
    143             Enum.valueOf(c, "ZERO");
    144             Enum.valueOf(c, "ONE");
    145             Enum.valueOf(c, "TWO");
    146             Enum.valueOf(c, "THREE");
    147             Enum.valueOf(c, "FOUR");
    148             Enum.valueOf(c, "FIVE");
    149             Enum.valueOf(c, "SIX");
    150             Enum.valueOf(c, "SEVEN");
    151             Enum.valueOf(c, "EIGHT");
    152             Enum.valueOf(c, "NINE");
    153             Enum.valueOf(c, "TEN");
    154             Enum.valueOf(c, "ELEVEN");
    155             Enum.valueOf(c, "TWELVE");
    156             Enum.valueOf(c, "THIRTEEN");
    157             Enum.valueOf(c, "FOURTEEN");
    158             Enum.valueOf(c, "FIFTEEN");
    159             Enum.valueOf(c, "SIXTEEN");
    160             Enum.valueOf(c, "SEVENTEEN");
    161             Enum.valueOf(c, "EIGHTEEN");
    162             Enum.valueOf(c, "NINETEEN");
    163         }
    164 
    165         return iters * 20;
    166     }
    167 
    168     static public int test3(int iters) {
    169         Class<OtherPackagePublicEnum> c = OtherPackagePublicEnum.class;
    170         for (int i = iters; i > 0; i--) {
    171             Enum.valueOf(c, "ZERO");
    172             Enum.valueOf(c, "ONE");
    173             Enum.valueOf(c, "TWO");
    174             Enum.valueOf(c, "THREE");
    175             Enum.valueOf(c, "FOUR");
    176             Enum.valueOf(c, "FIVE");
    177             Enum.valueOf(c, "SIX");
    178             Enum.valueOf(c, "SEVEN");
    179             Enum.valueOf(c, "EIGHT");
    180             Enum.valueOf(c, "NINE");
    181             Enum.valueOf(c, "TEN");
    182             Enum.valueOf(c, "ELEVEN");
    183             Enum.valueOf(c, "TWELVE");
    184             Enum.valueOf(c, "THIRTEEN");
    185             Enum.valueOf(c, "FOURTEEN");
    186             Enum.valueOf(c, "FIFTEEN");
    187             Enum.valueOf(c, "SIXTEEN");
    188             Enum.valueOf(c, "SEVENTEEN");
    189             Enum.valueOf(c, "EIGHTEEN");
    190             Enum.valueOf(c, "NINETEEN");
    191         }
    192 
    193         return iters * 20;
    194     }
    195 }
    196