/* Lecture Code 5.3 * * Time trial of the STL stack vs. the CS106 Stack. */ #include "genlib.h" #include "stack.h" #include #include #include using namespace std; const int NUM_ELEMS = 10000; 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 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("Hello!"); 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 >(); RunTest >(); return 0; }