import java.util.Random; public class EnumTest { public static enum Type { A, B, C, D, E, F, G, H, I, J } public static final int AA = 0; public static final int BB = 1; public static final int CC = 2; public static final int DD = 3; public static final int EE = 4; public static final int FF = 5; public static final int GG = 6; public static final int HH = 7; public static final int II = 8; public static final int JJ = 9; private static int[] genRandomInts(int count) { int[] is = new int[count]; Random r = new Random(); for (int i = 0; i < count; i++) { is[i] = r.nextInt(10); } return is; } private static Type[] genRandomTypes(int count) { Type[] ts = new Type[count]; Random r = new Random(); for (int i = 0; i < count; i++) { int temp = r.nextInt(10); if (temp == AA) { ts[i] = Type.A; } else if (temp == BB) { ts[i] = Type.B; } else if (temp == CC) { ts[i] = Type.C; } else if (temp == DD) { ts[i] = Type.D; } else if (temp == EE) { ts[i] = Type.E; } else if (temp == FF) { ts[i] = Type.F; } else if (temp == GG) { ts[i] = Type.G; } else if (temp == HH) { ts[i] = Type.H; } else if (temp == II) { ts[i] = Type.I; } else if (temp == JJ) { ts[i] = Type.J; } } return ts; } private static void testInt(int count) { int x = 0; int[] rs = genRandomInts(count); long t1 = System.currentTimeMillis(); for (int r : rs) { if (r == AA) { x = 10; } else if (r == BB) { x = 11; } else if (r == CC) { x = 12; } else if (r == DD) { x = 13; } else if (r == EE) { x = 14; } else if (r == FF) { x = 15; } else if (r == GG) { x = 16; } else if (r == HH) { x = 17; } else if (r == II) { x = 18; } else if (r == JJ) { x = 19; } else { } } long t2 = System.currentTimeMillis(); System.out.println("time for int[" + count + "] (ms): " + (t2 - t1)); } private static void testEnum(int count) { int x = 0; Type[] ts = genRandomTypes(count); long t1 = System.currentTimeMillis(); for (Type t : ts) { switch(t) { case A: x = 10; break; case B: x = 11; break; case C: x = 12; break; case D: x = 13; break; case E: x = 14; break; case F: x = 15; break; case G: x = 16; break; case H: x = 17; break; case I: x = 18; break; case J: x = 19; break; default: break; } } long t2 = System.currentTimeMillis(); System.out.println("time for type[" + count + "] (ms): " + (t2 - t1)); } public static void main(String args[]) { int[] cs = new int[] { 10, 100, 1000, 10000, 100000, 1000000, 10000000 }; for (int c : cs) { testInt(c); testEnum(c); } } }