fork download
  1. #include <iostream>
  2. #include <ctime>
  3. using std::cout;
  4. using std::endl;
  5.  
  6. enum class func_method {log_or, mult};
  7.  
  8. template <typename test_type, func_method method>
  9. void test_func()
  10. {
  11. const unsigned long end = 2000000000;
  12.  
  13. volatile test_type x = 0;
  14. volatile test_type y = 1;
  15. unsigned int sum = 0;
  16.  
  17. auto clock_begin = std::clock();
  18. if (method == func_method::log_or)
  19. for (unsigned long i = 0; i < end; ++i)
  20. {
  21. test_type a = x;
  22. test_type b = y;
  23. if (a == 0 || b == 0)
  24. ++sum;
  25. }
  26. else if (method == func_method::mult)
  27. for (unsigned long i = 0; i < end; ++i)
  28. {
  29. test_type a = x;
  30. test_type b = y;
  31. if (a * b == 0)
  32. ++sum;
  33. }
  34. else
  35. cout << "Invalid method" << endl;
  36. auto clock_end = std::clock();
  37.  
  38. cout << "sum: " << sum << endl;
  39. cout << "time: " << double(clock_end - clock_begin) / CLOCKS_PER_SEC << endl;
  40. }
  41.  
  42. int main()
  43. {
  44. typedef long long test_type;
  45.  
  46. test_func<test_type, func_method::mult>();
  47. test_func<test_type, func_method::log_or>();
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 2.85s 4888KB
stdin
Standard input is empty
stdout
sum:  2000000000
time: 0.918067
sum:  2000000000
time: 1.93048