fork download
  1. //
  2.  
  3. #include <chrono>
  4. #include <iostream>
  5. #include <vector>
  6. #include <tuple>
  7. #include <cassert>
  8. #include <cmath>
  9.  
  10. using namespace std;
  11. void benchmark();
  12. void benchmark_proper();
  13. int find_third_member_of_ptt(int z, int x);
  14.  
  15. int main()
  16. {
  17. benchmark();
  18. benchmark_proper();
  19.  
  20. }
  21.  
  22. ////////////////////////////////////////////////////////////////////////////////////////////////////
  23. // Benchmark Code
  24. ////////////////////////////////////////////////////////////////////////////////////////////////////
  25.  
  26. class timer
  27. {
  28. private:
  29. std::chrono::high_resolution_clock::time_point start_;
  30. public:
  31. timer()
  32. {
  33. reset();
  34. }
  35. void reset()
  36. {
  37. start_ = std::chrono::high_resolution_clock::now();
  38. }
  39. std::chrono::milliseconds elapsed() const
  40. {
  41. return std::chrono::duration_cast<std::chrono::milliseconds>(
  42. std::chrono::high_resolution_clock::now() - start_);
  43. }
  44. friend std::ostream &operator<<(std::ostream &sout, timer const &t)
  45. {
  46. return sout << t.elapsed().count() << "ms";
  47. }
  48. };
  49. static constexpr int max_triples = 2000;
  50.  
  51. void benchmark()
  52. {
  53. timer t;
  54. t.reset();
  55. vector<tuple<int,int,int>> ptt;
  56. for(int z = 1;; ++z)
  57. {
  58. for(int x = 1; x <= z; ++x)
  59. {
  60. for(int y = x; y <= z; ++y)
  61. {
  62. if(x*x + y*y == z*z)
  63. {
  64. ptt.push_back(make_tuple(x,y,z));
  65. if(ptt.size() == max_triples)
  66. goto done;
  67. }
  68. }
  69. }
  70. }
  71. done:
  72. std::cout << t << '\n';
  73. //for (const auto & triplet : ptt)
  74. // cout << get<0>(triplet) << " " << get<1>(triplet) << " " << get <2>(triplet) << endl;
  75. }
  76. // -1 == not found, yes boost optional would be better choice
  77. int find_third_member_of_ptt(int z, int x)
  78. {
  79. assert(z>x);
  80. const int diff = z*z - x*x;
  81. const double sqroot = sqrt(diff);
  82. const int candidate1 = (int)sqroot;
  83. const int candidate2 = (int)sqroot +1;
  84. // next 2 checks are the same and should be in a function
  85. if ( (candidate1 >= x) && //prevents duplicates
  86. (z*z == x*x + candidate1*candidate1))
  87. return candidate1;
  88. if ((candidate2 >= x) && //prevents duplicates
  89. (z*z == x*x + candidate2*candidate2))
  90. return candidate2;
  91. return -1;
  92. }
  93.  
  94. void benchmark_proper()
  95. {
  96. timer t;
  97. t.reset();
  98. vector<tuple<int,int,int>> ptt;
  99. for(int z = 1;; ++z)
  100. {
  101. for(int x = 1; x < z; ++x)
  102. {
  103. if (find_third_member_of_ptt(z,x) !=-1)
  104. {
  105. ptt.push_back(make_tuple(x,find_third_member_of_ptt(z,x),z)); // we are so fast we dont need to remember the rv of a function...
  106. if(ptt.size() == max_triples)
  107. goto done; // so great that we have goto in if now, goto == maintainable code, we could put it outside if but *I* know check only needs to be done if push_back occured
  108. }
  109. }
  110. }
  111. done:
  112. std::cout << t << '\n';
  113. //for (const auto & triplet : ptt)
  114. // cout << get<0>(triplet) << " " << get<1>(triplet) << " " << get <2>(triplet) << endl;
  115. }
  116.  
  117.  
Success #stdin #stdout 2.47s 3432KB
stdin
Standard input is empty
stdout
2390ms
82ms