fork download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <cstdint>
  4. using std::cout;
  5. using std::endl;
  6.  
  7. std::uint32_t rand_value;
  8.  
  9. std::uint32_t lcg()
  10. {
  11. const auto a = 22695477u;
  12. const auto c = 1u;
  13.  
  14. rand_value = a * rand_value + c;
  15. return rand_value;
  16. }
  17.  
  18. enum func_method {log_or, mult};
  19.  
  20. template <typename test_type, func_method method>
  21. void test_func()
  22. {
  23. const unsigned long end = 800'000'000;
  24. unsigned int sum = 0;
  25.  
  26. auto clock_begin = std::clock();
  27. if (method == func_method::log_or)
  28. for (unsigned long i = 0; i < end; ++i)
  29. {
  30. test_type a = lcg();
  31. test_type b = lcg();
  32. if (a == 0 || b == 0)
  33. ++sum;
  34. }
  35. else if (method == func_method::mult)
  36. for (unsigned long i = 0; i < end; ++i)
  37. {
  38. test_type a = lcg();
  39. test_type b = lcg();
  40. if (a * b == 0)
  41. ++sum;
  42. }
  43. else
  44. cout << "Invalid method" << endl;
  45. auto clock_end = std::clock();
  46.  
  47. cout << "sum: " << sum << endl;
  48. cout << "time: " << double(clock_end - clock_begin) / CLOCKS_PER_SEC << endl;
  49. }
  50.  
  51. int main()
  52. {
  53. typedef unsigned int test_type;
  54.  
  55. rand_value = 1;
  56. test_func<test_type, func_method::mult>();
  57. rand_value = 1;
  58. test_func<test_type, func_method::log_or>();
  59.  
  60. return 0;
  61. }
Success #stdin #stdout 3.9s 4992KB
stdin
Standard input is empty
stdout
sum:  0
time: 2.02724
sum:  0
time: 1.86795