fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6.  
  7. using namespace std;
  8.  
  9. int main() {
  10. vector<int> vi0;
  11. clock_t start;
  12. clock_t end;
  13. int size = pow(10, 6);
  14. cout << "Creating a vector with " << size << " random values" << endl;
  15. for (int i = 0; i < size; i++) {
  16. vi0.push_back(rand());
  17. }
  18. vector<int> vi(vi0);
  19. list<int> li = list<int>(vi0.begin(), vi0.end());
  20.  
  21. cout << "Starting vector sort" << endl;
  22. start = clock();
  23. sort(vi.begin(), vi.end());
  24. end = clock();
  25. double vecSort = (double)(end - start) / CLOCKS_PER_SEC;
  26.  
  27. cout << "Starting list sort" << endl;
  28. start = clock();
  29. li.sort();
  30. end = clock();
  31. double liSort = (double)(end - start) / CLOCKS_PER_SEC;
  32.  
  33. li = list<int>(vi0.begin(), vi0.end());
  34. cout << "Starting list-vector-list sort" << endl;
  35. start = clock();
  36. copy(li.begin(), li.end(), vi.begin());
  37. sort(vi.begin(), vi.end());
  38. copy(vi.begin(), vi.end(), li.begin());
  39. end = clock();
  40. double liVecSort = (double)(end - start) / CLOCKS_PER_SEC;
  41.  
  42. cout << "Vector sort: " << vecSort << " seconds.\n" <<
  43. "List sort: " << liSort << " seconds.\n" <<
  44. "List -> Vector sort -> List: " << liVecSort << " seconds." << endl;
  45. return 0;
  46. }
Success #stdin #stdout 0.51s 34624KB
stdin
Standard input is empty
stdout
Creating a vector with 1000000 random values
Starting vector sort
Starting list sort
Starting list-vector-list sort
Vector sort: 0.071162 seconds.
List sort: 0.226702 seconds.
List -> Vector sort -> List: 0.076124 seconds.