FileDocCategorySizeDatePackage
SwitchTest.javaAPI DocExample4111Tue May 07 13:06:36 BST 2002tuning.loop

SwitchTest

public class SwitchTest extends Object

Fields Summary
static int
SIZE
static final int[]
RETURNS
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)



    

  //Allow an argument to set the size of the loop.
  if (args.length != 0)
    SIZE = Integer.parseInt(args[0]);
  main(SIZE);
  main(SIZE);
public static voidmain(int SIZE)


  int result = 0;
  //run test looking for default
  long time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch1(i);
  System.out.println("Switch1 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch2(i);
  System.out.println("Switch2 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch3(i);
  System.out.println("Switch3 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch4(i);
  System.out.println("Switch4 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch1(i%2==0 ? 7 : 8);
  System.out.println("Switch1 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch2(i%2==0 ? 7 : 8);
  System.out.println("Switch2 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch3(i%2==0 ? 7 : 8);
  System.out.println("Switch3 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);

  result = 0;
  time = System.currentTimeMillis();
  for (int i = SIZE; i >=0 ; i--)
    result += switch4(i%2==0 ? 7 : 8);
  System.out.println("Switch4 took " + 
    (System.currentTimeMillis()-time) + " millis to get " + result);
public static intswitch1(int i)

  switch(i)
  {
    case 318: return 99;
    case 320: return 55;
    case 323: return -1;
    case 14: return 6;
    case 5: return 8;
    case 123456: return 12;
    case 7: return 15;
    case 8: return 29;
    case 9: return 11111;
    case 123457: return 12345;
    case 112233: return 6666;
    case 112235: return 9876;
    case 112237: return 12;
    default: return -1;
  }
public static intswitch2(int i)

  switch(i)
  {
    case 1: return 99;
    case 2: return 55;
    case 3: return -1;
    case 4: return 6;
    case 5: return 8;
    case 6: return 12;
    case 7: return 15;
    case 8: return 29;
    case 9: return 11111;
    case 10: return 12345;
    case 11: return 6666;
    case 12: return 9876;
    case 13: return 12;
    default: return -1;
  }
public static intswitch3(int i)

  switch(i)
  {
    case 318: return 99;
    case 319: break;
    case 320: return 55;
    case 321: break;
    case 322: break;
    case 323: return -1;
  }
  switch(i)
  {
    case 5: return 8;
    case 6: break;
    case 7: return 15;
    case 8: return 29;
    case 9: return 11111;
    case 10: break;
    case 11: break;
    case 12: break;
    case 13: break;
    case 14: return 6;
  }
  switch(i)
  {
    case 112233: return 6666;
    case 112234: break;
    case 112235: return 9876;
    case 112236: break;
    case 112237: return 12;
  }
  switch(i)
  {
    case 123456: return 12;
    case 123457: return 12345;
    default: return -1;
  }
public static intswitch4(int i)

  if (i < 1 || i > 13)
    return -1;
  else
    return RETURNS[i-1];