// Compare the execution times of inserting at the front vs. inserting // at the back of a vector. // // Usage: // ./ #include #include #include #include using namespace std; void insertManyFront(vector & v, int size) { for (int i = 0; i < size; i++) v.insert(v.begin(), rand() % size); } void insertManyBack(vector & v, int size) { for (int i = 0; i < size; i++) v.push_back(rand() % size); } int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: " << argv[0] << " \n"; exit(1); } int size = atoi(argv[1]); vector v,w; // initialize the random seed srand(time(NULL)); double elapsed_secs; // measure time to insert many elements in front clock_t begin = clock(); insertManyFront(v, size); clock_t end = clock(); elapsed_secs = static_cast(end - begin) / CLOCKS_PER_SEC; cout << "\nInsert at front elapsed: " << elapsed_secs << " seconds\n"; // measure time to insert many elements in the back begin = clock(); insertManyBack(w, size); end = clock(); elapsed_secs = static_cast(end - begin) / CLOCKS_PER_SEC; cout << "Insert back elapsed: " << elapsed_secs << " seconds\n\n"; return 0; }