fork download
  1. #include <string>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iostream>
  5. #include <ctime>
  6.  
  7. class SimpleObject
  8. {
  9. int _int;
  10. float _float;
  11.  
  12. public:
  13. SimpleObject() : _int(0), _float(0.0f) {}
  14.  
  15. int GetInt() { return _int; }
  16. float GetFloat() { return _float; }
  17.  
  18. void incrementInt() { ++_int; }
  19. void incrementFloat() { _float += 1.0f; }
  20. };
  21.  
  22. std::size_t getCount(const std::string& prompt)
  23. {
  24. std::string Input;
  25.  
  26. std::cout << prompt << "\n> ";
  27.  
  28. std::getline(std::cin, Input);
  29.  
  30. if (Input == "exit")
  31. return 0;
  32.  
  33. return std::stoi(Input);
  34. }
  35.  
  36. void doForLoop(std::vector<SimpleObject>& v, std::size_t iterations)
  37. {
  38. for (std::size_t i = 0; i < iterations; ++i)
  39. {
  40. for (auto it = v.begin(); it != v.end(); ++it)
  41. {
  42. it->incrementFloat();
  43. it->incrementInt();
  44. }
  45. }
  46. }
  47.  
  48. void doForEach(std::vector<SimpleObject>& v, std::size_t iterations)
  49. {
  50. for (std::size_t i = 0; i < iterations; ++i)
  51. for_each(v.begin(), v.end(), [](SimpleObject& o) { o.incrementFloat(); o.incrementInt(); });
  52. }
  53.  
  54. void doRangedFor(std::vector<SimpleObject>& v, std::size_t iterations)
  55. {
  56. for (std::size_t i = 0; i < iterations; ++i)
  57. for (auto & e : v)
  58. e.incrementFloat(), e.incrementInt();
  59. }
  60.  
  61. using func_type = void(*)(std::vector<SimpleObject>&, std::size_t);
  62. void test(const std::string& testName, func_type testme, std::size_t iterations, std::size_t objects)
  63. {
  64. std::vector<SimpleObject> v(objects);
  65.  
  66. clock_t begin = clock();
  67. testme(v, iterations);
  68. clock_t end = clock();
  69.  
  70. std::cout << "Test \"" << testName << "\" took " << (float(end) - begin) / CLOCKS_PER_SEC << " seconds.\n";
  71. }
  72.  
  73.  
  74. int main(int argc, char** argv)
  75. {
  76. std::size_t nIterations;
  77. std::size_t nObjects;
  78.  
  79. do
  80. {
  81. nIterations = getCount("Enter the number of iterations:");
  82. nObjects = getCount("Enter the number of objects in the container:");
  83.  
  84. if (nIterations && nObjects)
  85. {
  86. std::cout << "\n\nWe will be performing " << nIterations << " iterations on a container holding ";
  87. std::cout << nObjects << " objects.\n";
  88.  
  89. test("for loop", doForLoop, nIterations, nObjects);
  90. test("for each", doForEach, nIterations, nObjects);
  91. test("ranged for", doRangedFor, nIterations, nObjects);
  92. }
  93. } while (nIterations && nObjects);
  94. }
Success #stdin #stdout 2.08s 3436KB
stdin
1000000
500
0
0
stdout
Enter the number of iterations:
> Enter the number of objects in the container:
> 

We will be performing 1000000 iterations on a container holding 500 objects.
Test "for loop" took 0.69 seconds.
Test "for each" took 0.7 seconds.
Test "ranged for" took 0.69 seconds.
Enter the number of iterations:
> Enter the number of objects in the container:
>