/* Lecture Code 5.2
 *
 * Time trial of the STL stack<int> vs. the CS106 Stack<int>.
 * While some of the syntax here might be a bit tricky, most of the
 * template code is simply there so there is no code duplication
 * between the two trials.
 */

#include "genlib.h"
#include "stack.h"
#include <iostream>
#include <stack>
#include <ctime>
using namespace std;

const int NUM_ELEMS = 100000;
const int NUM_TESTS = 500;

/* If you're uncomfortable with template syntax, the next line simply
 * indicates that the next function is generic and will accept any
 * variable type.  However, the way it's written, the type must support
 * functions called "push" and "pop."
 */
template <typename StackType>
	void RunTest()
{
	
	/* The timer works by using the C runtime library clock function,
	 * which returns the current value of the CPU clock.  For more
	 * information, consult a C++ reference.
	 */
	clock_t start = clock();
	StackType myStack;

	for(int h = 0; h < NUM_TESTS; h++)
	{
		for(int i = 0; i < NUM_ELEMS; i++)
			myStack.push(i);
		for(int h = 0; h < NUM_ELEMS; h++)
			myStack.pop();
	}

	start = clock() - start;

	cout << "Test completed in ";
	cout << ((double)(start) / CLOCKS_PER_SEC);
	cout << " seconds." << endl;
}

int main()
{
	/* This next syntax is a bit cryptic, but it basically says
	 * to first run the test on the CS106 Stack and then to run
	 * it on the STL stack.
	 */
	RunTest<Stack<int> >();
	RunTest<stack<int> >();

	return 0;
}