// Simple-minded code to determine the performance of two string-processing
// strategies.
//
// CS 305 & 503 - Algorithms and Data Structures
// Fall '09


class 
string_proc {

  private interface
  StringProcessor {
    void go (int size);
    }

  private static void
  do_test(int iterations, int size) {

    // Time through the given number of iterations the string processors when
    // processing the given number of lines.

    run_test(iterations, size, "unnested",
      new StringProcessor() {
	public void 
	go(int size) {

	  // Fake reading the given number of strings and then fake processing
	  // them.

	  String read = new String("                                        " + 
				   "                                        ");
	  StringBuilder str = new StringBuilder();

	  while (size-- > 0) 
	    str.append(read);

	  for (int i = 0; i < str.length(); i++)
	    if (str.charAt(i) == '<') {
	      }
	  }
	});

    run_test(iterations, size, "nested",
      new StringProcessor() {
	public void 
	go(int size) {

	  // Fake reading the given number of strings and then fake processing
	  // them.

	  String read = new String("                                        " + 
				   "                                        ");
	  StringBuilder str = new StringBuilder();

	  while (size-- > 0) {
	    str.append(read);
	    for (int i = 0; i < str.length(); i++)
	      if (str.charAt(i) == '<') {
		str.setCharAt(i, ' ');
		}
	    }
	  }
	});
    }


  public static void
  main(String args[]) {

    // Write to std-out some performance data for various string processors
    // performing synthetic work.

    if (args.length != 2) {
      System.err.println("Two command-line arguments expected.");
      System.err.println("Command-line format is \"<size> <iters>\".");
      System.exit(1);
      }

    final int
      size = new Integer(args[0]).intValue(),
      iterations = new Integer(args[0]).intValue();

    do_test(iterations, size);
    }


  private static void
  run_test(int iterations, int size, String name, StringProcessor strprc) {

    // Run performance timing on the given string processor with the given
    // name.  The timing's over the given number of iterations; the processor
    // works on a string comprising the given number of lines.

    final EpisodeTimer et = new EpisodeTimer();

    while (iterations-- > 0) {
      et.start();
      strprc.go(size);
      et.stop();
      }

    System.out.printf("curve %s x %d y %d sd %d\n", name, size,
		      Math.round(et.average()), 
		      Math.round(et.stddev()));
    }
  }


// $Log:$


syntax highlighted by Code2HTML, v. 0.9.1