123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- // Compare the execution times of inserting at the front vs. inserting
- // at the back of a vector.
- //
- // Usage:
- // ./<exec name> <size of list>
-
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <algorithm>
- using namespace std;
-
-
- void insertManyFront(vector<int> & v, int size) {
- for (int i = 0; i < size; i++)
- v.insert(v.begin(), rand() % size);
- }
-
- void insertManyBack(vector<int> & 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] << " <size> \n";
- exit(1);
- }
- int size = atoi(argv[1]);
-
- vector<int> 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<double>(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<double>(end - begin) / CLOCKS_PER_SEC;
- cout << "Insert back elapsed: " << elapsed_secs << " seconds\n\n";
-
- return 0;
- }
-
-
|