Hello,

Some simple speed tests.

Regards
Qt Code:
  1. void f0(int n, std::vector<int> v);
  2. void f1(int n, std::vector<int> v);
  3. void f2(int n, std::vector<int> v);
  4. void f3(int n, std::vector<int> v);
  5. void f4(int n, int arr[], int size);
  6. void f5(int n, int arr[], int size);
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. //**********
  2. double d;
  3. //auto v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  4. std::vector<int> v = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  5. std::cout << "vector\n";
  6. //**********
  7. auto start_time = std::chrono::high_resolution_clock::now();
  8. //for (int t = 0; t < 100; t++)
  9. for (auto t = 0; t != 100; ++t)
  10. {
  11. for (int s = 0; s < 1000; s++)
  12. f0 (1000, v);
  13. }
  14. auto end_time = std::chrono::high_resolution_clock::now();
  15. auto time = end_time - start_time;
  16. d = std::chrono::duration_cast<std::chrono::microseconds>(time).count();
  17. std::cout << "f0 took " << (d / 100) << " microseconds.\n";
  18. //**********
  19. auto start_time6 = std::chrono::high_resolution_clock::now();
  20. for (int t = 0; t < 100; t++)
  21. //for (auto t = 0; t != 100; ++t)
  22. {
  23. for (int s = 0; s < 1000; s++)
  24. f0 (1000, v);
  25. }
  26. auto end_time6 = std::chrono::high_resolution_clock::now();
  27. auto time6 = end_time6 - start_time6;
  28. d = std::chrono::duration_cast<std::chrono::microseconds>(time6).count();
  29. std::cout << "f6 took " << (d / 100) << " microseconds.\n";
  30. //**********
  31. auto start_time1 = std::chrono::high_resolution_clock::now();
  32. for (int t = 0; t < 100; t++)
  33. {
  34. for (int s = 0; s < 1000; s++)
  35. f1 (1000, v);
  36. }
  37. auto end_time1 = std::chrono::high_resolution_clock::now();
  38. auto time1 = end_time1 - start_time1;
  39. d = std::chrono::duration_cast<std::chrono::microseconds>(time1).count();
  40. std::cout << "f1 took " << (d / 100) << " microseconds.\n";
  41. //**********
  42. auto start_time2 = std::chrono::high_resolution_clock::now();
  43. for (int t = 0; t < 100; t++)
  44. {
  45. for (int s = 0; s < 1000; s++)
  46. f2 (1000, v);
  47. }
  48. auto end_time2 = std::chrono::high_resolution_clock::now();
  49. auto time2 = end_time2 - start_time2;
  50. d = std::chrono::duration_cast<std::chrono::microseconds>(time2).count();
  51. std::cout << "f2 took " << (d / 100) << " microseconds reverse.\n";
  52. //**********
  53. auto start_time3 = std::chrono::high_resolution_clock::now();
  54. for (int t = 0; t < 100; t++)
  55. {
  56. for (int s = 0; s < 1000; s++)
  57. f3 (1000, v);
  58. }
  59. auto end_time3 = std::chrono::high_resolution_clock::now();
  60. auto time3 = end_time3 - start_time3;
  61. d = std::chrono::duration_cast<std::chrono::microseconds>(time3).count();
  62. std::cout << "f3 took " << (d / 100)
  63. << " microseconds.\n";
  64. //**********
  65. int a[] = {10, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  66. std::cout << "\narray\n";
  67. //**********
  68. auto start_time4 = std::chrono::high_resolution_clock::now();
  69. for (int t = 0; t < 100; t++)
  70. {
  71. for (int s = 0; s < 1000; s++)
  72. f4 (1000, a, (sizeof(a)/sizeof(a[0])));
  73. }
  74. auto end_time4 = std::chrono::high_resolution_clock::now();
  75. auto time4 = end_time4 - start_time4;
  76. d = std::chrono::duration_cast<std::chrono::microseconds>(time4).count();
  77. std::cout << "f4 took "
  78. << (d / 100)
  79. << " microseconds.\n";
  80. //**********
  81. auto start_time5 = std::chrono::high_resolution_clock::now();
  82. for (int t = 0; t < 100; t++)
  83. {
  84. for (int s = 0; s < 1000; s++)
  85. f5 (1000, a, (sizeof(a)/sizeof(a[0])));
  86. }
  87. auto end_time5 = std::chrono::high_resolution_clock::now();
  88. auto time5 = end_time5 - start_time5;
  89. d = std::chrono::duration_cast<std::chrono::microseconds>(time5).count();
  90. std::cout << "f5 took "
  91. << (d / 100)
  92. << " microseconds reverse.\n";
  93. //**********
  94. std::cout << "\n";
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. #pragma GCC push_options
  2. #pragma GCC optimize ("O0")
  3. void MainWindow::f0(int n, std::vector<int> v) {
  4. int k;
  5. for (int i = 0; i < n; i++)
  6. {
  7. for (const int& i : v) // access by const reference
  8. {
  9. k = i;
  10. }
  11. }
  12. }
  13. #pragma GCC pop_options
  14.  
  15. #pragma GCC push_options
  16. #pragma GCC optimize ("O0")
  17. void MainWindow::f1(int n, std::vector<int> v) {
  18. int k;
  19. for (int i = 0; i < n; i++)
  20. {
  21. for (auto i : v) // access by value, the type of i is int
  22. {
  23. k = i;
  24. }
  25. }
  26. }
  27. #pragma GCC pop_options
  28.  
  29. #pragma GCC push_options
  30. #pragma GCC optimize ("O0")
  31. void MainWindow::f2(int n, std::vector<int> v) {
  32. int k;
  33. for (int i = 0; i < n; i++)
  34. {
  35. for (auto i = v.end() - 1; i >= v.begin(); --i) //reverse
  36. {
  37. k = *i;
  38. //qDebug() << k << *i;
  39. }
  40. }
  41. }
  42. #pragma GCC pop_options
  43.  
  44. #pragma GCC push_options
  45. #pragma GCC optimize ("O0")
  46. void MainWindow::f3(int n, std::vector<int> v) {
  47. int k;
  48. for (int i = 0; i < n; i++)
  49. {
  50. for (int i = 0; i <= v.size(); i++) //conventional style
  51. {
  52. //k = v.at(i); //ok both of these are similar times
  53. k = v[i]; //ok
  54. //qDebug() << k << i;
  55. }
  56. }
  57. }
  58. #pragma GCC pop_options
  59.  
  60. #pragma GCC push_options
  61. #pragma GCC optimize ("O0")
  62. void MainWindow::f4(int n, int arr[], int size) {
  63. int k;
  64. for (int i = 0; i < n; i++)
  65. {
  66. for (int i = 0; i <= size - 1; i++)
  67. {
  68. k = arr[i]; //ok
  69. //qDebug() << k << i << j;
  70. }
  71. }
  72. }
  73. #pragma GCC pop_options
  74.  
  75. #pragma GCC push_options
  76. #pragma GCC optimize ("O0")
  77. void MainWindow::f5(int n, int arr[], int size) {
  78. int k;
  79. for (auto i = 0; i != n; ++i)
  80. {
  81. for (auto i = size - 1; i >= 0; --i) //reverse
  82. {
  83. k = arr[i]; //ok
  84. //qDebug() << arr[i];
  85. }
  86. }
  87. }
  88. #pragma GCC pop_options
To copy to clipboard, switch view to plain text mode 
Qt Code:
  1. vector
  2. f0 took 171214 microseconds.
  3. f6 took 171078 microseconds.
  4. f1 took 147593 microseconds.
  5. f2 took 154707 microseconds reverse.
  6. f3 took 87834.4 microseconds.
  7.  
  8. array
  9. f4 took 71596.9 microseconds.
  10. f5 took 66486.2 microseconds reverse.
To copy to clipboard, switch view to plain text mode