package javakurs.musterloesung.uebung11;

import java.util.ArrayList;
import java.util.List;

/**
 * {@link CacheDemo} demonstrates the effect of caching the result of the
 * extend method in {@link TopDownParser}.
 *
 * @author Joerg Steffen, DFKI
 * @version $Id: CacheDemo.java,v 1.1 2011/01/26 15:50:17 steffen Exp $
 */
public final class CacheDemo {

  /**
   * Would create a new instance of {@link CacheDemo}. Not to be used.
   */
  private CacheDemo() {

    // private constructor to enforce noninstantiability
  }


  /**
   * The main method; requires no arguments.
   *
   * @param args
   *          the arguments; not used here
   */
  public static void main(String[] args) {

    TopDownParser tdParser =
      new TopDownParser("data/uebung11/cacheDemo.gram");

    // setup input
    String[] testInputs = { "x", "y", "z", "f", "g" };

    List<String> input = new ArrayList<String>();
    for (int i = 0; i < testInputs.length; i++) {
      input.add(testInputs[i]);
    }
    // parse without cache
    System.out.println("without cache:");
    tdParser.setCacheFlag(false);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      // parse input
      tdParser.recognize(input);
    }
    System.out.println(System.currentTimeMillis() - start + " milliseconds");
    // parse with cache
    System.out.println("with cache:");
    tdParser.setCacheFlag(true);
    start = System.currentTimeMillis();
    for (int i = 0; i < 100000; i++) {
      // parse input
      tdParser.recognize(input);
    }
    System.out.println(System.currentTimeMillis() - start + " milliseconds");
  }
}
