FileDocCategorySizeDatePackage
TryCatchCostTest.javaAPI DocExample1917Sun Jun 23 16:27:44 BST 2002tuning.exception

TryCatchCostTest

public class TryCatchCostTest extends Object

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

    maintest(args);
    if (args.length > 0)
      maintest(args);
  
public static voidmaintest(java.lang.String[] args)

    Integer i1 = new Integer(3);
    Integer i2 = new Integer(4);
    Boolean b1 = new Boolean(true);
    Boolean b2 = new Boolean(false);
    int REPEAT = 50000;
    int FACTOR = 1000;
    boolean res = false;

    System.out.println(test1(i1));
    System.out.println(test1(b1));
    System.out.println(test2(i1));
    System.out.println(test2(b1));

    long time = System.currentTimeMillis();
    for (int j = REPEAT*FACTOR; j > 0 ; j--)
      res = test1(res ? i1 : i2);
    time = System.currentTimeMillis() - time;
    System.out.println("test1(i) took " + time);

    time = System.currentTimeMillis();
    for (int j = REPEAT; j > 0 ; j--)
      res = test1(res ? b1 : b2);
    time = System.currentTimeMillis() - time;
    System.out.println("test1(b) took " + time);
    System.out.println("Note that test1(b) was executed " + FACTOR + " times less than the other tests");

    time = System.currentTimeMillis();
    for (int j = REPEAT*FACTOR; j > 0 ; j--)
      res = test2(res ? i1 : i2);
    time = System.currentTimeMillis() - time;
    System.out.println("test2(i) took " + time);

    time = System.currentTimeMillis();
    for (int j = REPEAT*FACTOR; j > 0 ; j--)
      res = test2(res ? b1 : b2);
    time = System.currentTimeMillis() - time;
    System.out.println("test2(b) took " + time);
  
public static booleantest1(java.lang.Object o)

    try
    {
      Integer i = (Integer) o;
      return false;
    }
    catch (Exception e)
    {
      return true;
    }
  
public static booleantest2(java.lang.Object o)

    if (o instanceof Number)
      return false;
    else
      return true;