fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <chrono>
  4. #include <algorithm>
  5.  
  6. template <typename timepoint_type>
  7. void display_time_difference(timepoint_type start, timepoint_type end)
  8. {
  9. using namespace std::chrono;
  10.  
  11. std::cout << "Took " << duration_cast<milliseconds>(end - start).count() ;
  12. std::cout << " ms." << std::endl;
  13. }
  14.  
  15. int main()
  16. {
  17. const unsigned outer_size = 10000;
  18. const unsigned inner_size = 5000;
  19.  
  20.  
  21. std::cout << "1) a 2D dynamic array of size " << outer_size ;
  22. std::cout << " x " << inner_size << std::endl;
  23.  
  24. auto start = std::chrono::high_resolution_clock::now();
  25.  
  26. int** array2D = new int*[outer_size];
  27. for (unsigned i = 0; i < outer_size; i++)
  28. {
  29. array2D[i] = new int[inner_size];
  30. std::fill(array2D[i], array2D[i] + inner_size, 1);
  31. }
  32.  
  33. auto end = std::chrono::high_resolution_clock::now();
  34. display_time_difference(start, end);
  35.  
  36. for (unsigned i = 0; i < outer_size; ++i)
  37. delete [] array2D[outer_size-i-1];
  38. delete [] array2D;
  39.  
  40.  
  41. std::cout << "2) a 2D vector of size " << outer_size;
  42. std::cout << " x " << inner_size << ", using resize" << std::endl;
  43. start = std::chrono::high_resolution_clock::now();
  44. {
  45. std::vector<std::vector<int> > vec2D;
  46. vec2D.resize(outer_size);
  47. for (unsigned i = 0; i < outer_size; i++)
  48. {
  49. vec2D[i].resize(inner_size);
  50. std::fill(vec2D[i].begin(), vec2D[i].end(), 1);
  51. }
  52.  
  53. end = std::chrono::high_resolution_clock::now();
  54. }
  55. display_time_difference(start, end);
  56.  
  57.  
  58. std::cout << "3) a 2D vector of size " << outer_size;
  59. std::cout << " x " << inner_size << ", using push_back" << std::endl;
  60. start = std::chrono::high_resolution_clock::now();
  61. {
  62. std::vector<std::vector<int> > vec2D;
  63. for (unsigned i = 0; i < outer_size; ++i)
  64. {
  65. vec2D.push_back(std::vector<int>());
  66. std::vector<int>& v = vec2D.back();
  67. for (unsigned j = 0; j < inner_size; ++j)
  68. v.push_back(1);
  69. }
  70. end = std::chrono::high_resolution_clock::now();
  71. }
  72. display_time_difference(start, end);
  73.  
  74.  
  75. std::cout << "4) a 2D vector of size " << outer_size;
  76. std::cout << " x " << inner_size << ", using constructors" << std::endl;
  77. start = std::chrono::high_resolution_clock::now();
  78. {
  79. std::vector<std::vector<int>> vec2D(outer_size, std::vector<int>(inner_size, 1));
  80. end = std::chrono::high_resolution_clock::now();
  81. }
  82. display_time_difference(start, end);
  83. }
Success #stdin #stdout 1.15s 3568KB
stdin
Standard input is empty
stdout
1) a 2D dynamic array of size 10000 x 5000
Took 185 ms.
2) a 2D vector of size 10000 x 5000, using resize
Took 250 ms.
3) a 2D vector of size 10000 x 5000, using push_back
Took 465 ms.
4) a 2D vector of size 10000 x 5000, using constructors
Took 183 ms.