FileDocCategorySizeDatePackage
TryCatchTimeTest.javaAPI DocExample2270Wed Dec 30 23:01:34 GMT 1998tuning.exception

TryCatchTimeTest

public class TryCatchTimeTest extends Object

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

    int REPEAT = 10000000;
    if (args.length == 1)
      REPEAT = Integer.parseInt(args[0]);
    System.out.println("Using a repeat value (number of loop iterations) of " + REPEAT);

    Object[] xyz = {new Integer(3), new Integer(10101), new Integer(67)};
    boolean res;
    long time = System.currentTimeMillis();
    res = try_catch_in_loop(REPEAT, xyz);
    System.out.println("try catch in loop took     " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    res = try_catch_not_in_loop(REPEAT, xyz);
    System.out.println("try catch not in loop took " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    res = try_catch_not_in_loop(REPEAT, xyz);
    System.out.println("try catch not in loop took " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    res = try_catch_in_loop(REPEAT, xyz);
    System.out.println("try catch in loop took     " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    res = try_catch_not_in_loop(REPEAT, xyz);
    System.out.println("try catch not in loop took " + (System.currentTimeMillis() - time));

    time = System.currentTimeMillis();
    res = try_catch_in_loop(REPEAT, xyz);
    System.out.println("try catch in loop took     " + (System.currentTimeMillis() - time));
  
public static booleantry_catch_in_loop(int repeat, java.lang.Object[] o)

    Integer i[] = new Integer[3];
    for (int j = repeat; j > 0; j--)
    {
      try
      {
        i[0] = (Integer) o[(j+1)%2];
        i[1] = (Integer) o[j%2];
        i[2] = (Integer) o[(j+2)%2];
      }
      catch (Exception e)
      {
        return true;
      }
    }
    return false;
  
public static booleantry_catch_not_in_loop(int repeat, java.lang.Object[] o)

    Integer i[] = new Integer[3];
    try
    {
      for (int j = repeat; j > 0; j--)
      {
        i[0] = (Integer) o[(j+1)%2];
        i[1] = (Integer) o[j%2];
        i[2] = (Integer) o[(j+2)%2];
      }
      return false;
    }
    catch (Exception e)
    {
      return true;
    }