// Simple-minded code to determine the performance of two string-processing
// strategies.
//
// CS 306 - Computer Algorithms II
// Fall '07

#include <iostream>
#include <string>
#include "cl-options.h"
#include "episode-timer.h"


// Deja vu c++ style.

   static void run_test(
     unsigned, unsigned, void (*)(unsigned), const char * const)


static void 
unnested(unsigned size)
  
  // Fake reading the given number of strings and then fake processing them.

  const std::string read(80, ' ')
  std::string str

  while size-- > 0 
    str += read

  for unsigned i = 0; i < str.size(); i++
    if str[i] == '<'


static void 
nested(unsigned size)
  
  // Fake reading the given number of strings and then fake processing them.

  const std::string read(80, ' ')
  std::string str

  while size-- > 0
    str += read
    for unsigned i = 0; i < str.size(); i++
      if str[i] == '<'
	str[i] = ' '


static void
do_test(unsigned iterations, unsigned size)

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

  run_test(iterations, size, unnested, "unnested")
  run_test(iterations, size, nested, "nested")


int 
main(int argc, char * argv[])

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

  cl_options opts
  opts['i'] = "10"
  opts['s'] = "100"
  const unsigned o = get_cl_options(argc, argv, opts)
  
  if o != static_cast<unsigned>(argc)
    std::cerr << "Too many command-line arguments.\n"
    std::cerr << "Command format is \"" << argv[0] << " [n]\".\n"
    exit(EXIT_FAILURE)

  unsigned 
    size = atoi(opts['s']),
    iterations = atoi(opts['i'])

  do_test(iterations, size)


static void
run_test(
  unsigned iterations, unsigned size, 
  void (* strfun)(unsigned), const char * const name)

  // 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.

  episode_timer et

  while iterations-- > 0
    et.start()
    strfun(size)
    et.stop()

  printf("curve %s x %u y %u sd %u\n", name, size,
	 static_cast<unsigned>(round(et.average())), 
	 static_cast<unsigned>(round(et.stddev())))


// $Log: string-proc.cc,v $
// Revision 1.1  2007-10-05 14:56:19  rclayton
// Created.
//



syntax highlighted by Code2HTML, v. 0.9.1