#ifndef _episode_timer_h_defined_
#define _episode_timer_h_defined_

#include <vector>
#include <numeric>
#include <cmath>
#include <cassert>
#include "stopwatch.h"


class episode_timer

  public:

    // Return the average of the collected episode times; die if there's less
    // than 1 episode.

       double average(void) const
	 const unsigned n = e_times.size()
	 assert(n > 0)
	 return static_cast<double>(total())/static_cast<double>(n)


    // Clear this episode timer.

       void clear()
	 e_times.clear()


    // Return the number of episodes timed so far.

       unsigned count() const
	 return e_times.size()


    // Create a new episode timer initially sized to hold the given number of
    // episodes.

       episode_timer(unsigned size = 1024)
	 e_times.reserve(size)


    // Start this episode timer.

       void start(void)
	 sw.start()


    // Return the standard deviation of the collected episode times; die if
    // there's less than two episodes.

       double stddev() const

	 const unsigned n = e_times.size()
	 assert(n > 1)

	 double std = 0.0
	 for unsigned i = 0; i < n; i++
	   const double d = e_times[i]
	   std += d*d

	 const double avg = average()

	 return sqrt((std - n*avg*avg)/(n - 1))


    // Stop this episode timer.

       void stop(void)
	 sw.stop()
	 e_times.push_back(sw.elapsed())


    // Return the sum of all episode times captured since the most recent
    // clear() call or from the beginning.

       unsigned total() const
	 return std::accumulate(e_times.begin(), e_times.end(), 0)


  private:

    std::vector<unsigned> e_times
    stopwatch sw

#endif


// $Log: episode-timer.h,v $
// Revision 1.1  2004/03/10 14:05:43  rclayton
// Initial revision
//


syntax highlighted by Code2HTML, v. 0.9.1