/* Lecture Code 2.1
 *
 * Time trial of the STL stack<string> vs. the CS106 Stack<string>.
 */

#include "genlib.h"
#include "stack.h"
#include <iostream>
#include <stack>
#include <ctime>
using namespace std;

const size_t kNumElems = 10000;
const size_t kNumTests = 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 (size_t h = 0; h < kNumTests; h++) {
		for (size_t i = 0; i < kNumElems; i++)
			myStack.push("Hello!");
		for (size_t h = 0; h < kNumElems; 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<string> >();
	RunTest< stack<string> >();

	return 0;
}