// Simple-minded code to determine the performance of a simple-minded sequence
// class with amortized and doubling data-storage growth.
//
// CS 306 - Computer Algorithms II
// Fall '07

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


// Deja vu c++ style.

   static void run_test(unsigned, unsigned, unsigned)


struct amortized_sequence

  unsigned next, max, a
  int * data

  amortized_sequence(unsigned a)

  void append()
    if next >= max
      int * new_data = new int [max += a]
      memcpy(new_data, data, sizeof(int)*next)
      delete [] data
      data = new_data
    data[next++] = 0


struct doubling_sequence

  unsigned next, max
  int * data

  doubling_sequence()

  void append()
    if next >= max
      int * new_data = new int [max *= 2]
      memcpy(new_data, data, sizeof(int)*next)
      delete [] data
      data = new_data
    data[next++] = 0


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"
  opts['a'] = "1"
  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)

  run_test(atoi(opts['a']), atoi(opts['s']), atoi(opts['i']))


static void
run_test(unsigned a, unsigned input_size, unsigned iterations)

  // 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
    if a
      amortized_sequence s(a)
      et.start()
      for unsigned i = 0; i < input_size; i++
	s.append()
      et.stop()
    else
      doubling_sequence s
      et.start()
      for unsigned i = 0; i < input_size; i++
	s.append()
      et.stop()

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


// $Log: amoseq.cc,v $
// Revision 1.1  2007-10-07 00:26:56  rclayton
// Created.
//



syntax highlighted by Code2HTML, v. 0.9.1