/* Lecture Code 5.5 * * Time trial of vector vs deque for the push_front (or insert(container.begin())) functions. * Refer to 5.2 or 5.3 for more info on tricky syntax. */ #include #include #include #include using namespace std; const int NUM_ELEMS = 10000; const int NUM_TESTS = 100; void RunVectorTest() { clock_t start = clock(); vector myContainer; for(int h = 0; h < NUM_TESTS; h++) { for(int i = 0; i < NUM_ELEMS; i++) myContainer.insert(myContainer.begin(), i); for(int h = 0; h < NUM_ELEMS; h++) myContainer.erase(myContainer.begin()); } start = clock() - start; cout << "Vector finished in "; cout << ((double)(start) / CLOCKS_PER_SEC); cout << " seconds." << endl; } void RunDequeTest() { clock_t start = clock(); deque myContainer; for(int h = 0; h < NUM_TESTS; h++) { for(int i = 0; i < NUM_ELEMS; i++) myContainer.push_front(i); for(int h = 0; h < NUM_ELEMS; h++) myContainer.pop_front(); } start = clock() - start; cout << "Deque finished in "; cout << ((double)(start) / CLOCKS_PER_SEC); cout << " seconds." << endl; } int main() { RunVectorTest(); RunDequeTest(); return 0; }