fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int testVector(int count) {
  9. vector<int> v;
  10. for (int i=0; i<count; i++) v.push_back(count-i);
  11. return v.size();
  12. }
  13.  
  14. int testVector2(int count) {
  15. vector<int> v;
  16. for (int i=0; i<count; i++) v.insert(v.begin(), 1, i);
  17. return v.size();
  18. }
  19.  
  20. int testList(int count) {
  21. list<int> v;
  22. for (int i=0; i<count; i++) v.push_back(count-i);
  23. return v.size();
  24. }
  25.  
  26. int testList2(int count) {
  27. list<int> v;
  28. for (int i=0; i<count; i++) v.push_front(count-i);
  29. return v.size();
  30. }
  31.  
  32. int main() {
  33. const int loopcnt = 1000000;
  34. const int loopcnt2 = 100000;
  35. clock_t sw;
  36. sw = clock();
  37. int c = testVector(loopcnt);
  38. sw = clock() - sw;
  39. cout << "vector back : " << c << " : " << sw / (double) CLOCKS_PER_SEC << endl;
  40. sw = clock();
  41. c = testVector2(loopcnt2);
  42. sw = clock() - sw;
  43. cout << "vector front : " << c << " : " << sw / (double) CLOCKS_PER_SEC << endl;
  44. sw = clock();
  45. c = testList(loopcnt);
  46. sw = clock() - sw;
  47. cout << "list back : " << c << " : " << sw / (double) CLOCKS_PER_SEC << endl;
  48. sw = clock();
  49. c = testList2(loopcnt);
  50. sw = clock() - sw;
  51. cout << "list front : " << c << " : " << sw / (double) CLOCKS_PER_SEC << endl;
  52. return 0;
  53. }
  54.  
Success #stdin #stdout 3.92s 19008KB
stdin
Standard input is empty
stdout
vector back : 1000000 : 0
vector front : 100000 : 3.69
list back : 1000000 : 0.13
list front : 1000000 : 0.08