fork download
  1. #include <algorithm>
  2. #include <chrono>
  3. #include <iostream>
  4. #include <random>
  5.  
  6. template <typename X>
  7. const X& max_3_left( const X& a, const X& b, const X& c )
  8. {
  9. return std::max( std::max( a, b ), c );
  10. }
  11.  
  12. template <typename X>
  13. const X& max_3_right( const X& a, const X& b, const X& c )
  14. {
  15. return std::max( a, std::max( b, c ) );
  16. }
  17.  
  18. int main()
  19. {
  20. std::random_device r;
  21. std::default_random_engine e1( r() );
  22. std::uniform_int_distribution<int> uniform_dist( 1, 6 );
  23. std::vector<int> numbers;
  24. for ( int i = 0; i < 1000; ++i )
  25. numbers.push_back( uniform_dist( e1 ) );
  26.  
  27. auto start1 = std::chrono::high_resolution_clock::now();
  28. int sum1 = 0;
  29. for ( int i = 0; i < 1000; ++i )
  30. for ( int j = 0; j < 1000; ++j )
  31. for ( int k = 0; k < 100; ++k )
  32. sum1 += max_3_left( numbers[i], numbers[j], numbers[k] );
  33. auto finish1 = std::chrono::high_resolution_clock::now();
  34. std::cout << "left " << sum1 << " " <<
  35. std::chrono::duration_cast<std::chrono::microseconds>(finish1 - start1).count()
  36. << " us" << std::endl;
  37.  
  38. auto start2 = std::chrono::high_resolution_clock::now();
  39. int sum2 = 0;
  40. for ( int i = 0; i < 1000; ++i )
  41. for ( int j = 0; j < 1000; ++j )
  42. for ( int k = 0; k < 100; ++k )
  43. sum2 += max_3_right( numbers[i], numbers[j], numbers[k] );
  44. auto finish2 = std::chrono::high_resolution_clock::now();
  45. std::cout << "right " << sum2 << " " <<
  46. std::chrono::duration_cast<std::chrono::microseconds>(finish2 - start2).count()
  47. << " us" << std::endl;
  48. }
Success #stdin #stdout 0.9s 3460KB
stdin
Standard input is empty
stdout
left  495109256 374104 us
right 495109256 525929 us