fork download
  1. #include <array>
  2. #include <chrono>
  3. #include <iostream>
  4. #include <random>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. void maxArray(double* x, double* y) {
  12. for (int i = 0; i < 65536; i++) {
  13. if (y[i] > x[i]) x[i] = y[i];
  14. }
  15. }
  16.  
  17.  
  18. void maxArrayCPPP(std::array<double,65536>& x, std::array<double,65536>& y)
  19. {
  20. for (auto& _x : x)
  21. {
  22. const auto dist = &_x - &x[0];
  23. const auto& _y = y[dist];
  24. if (_y> _x) _x=_y;
  25. }
  26. }
  27.  
  28.  
  29.  
  30. int main()
  31. {
  32. array<double,65536> arr1;
  33. array<double,65536> arr2;
  34. std::uniform_real_distribution<double> unif(0,100);
  35. std::mt19937 rand_engine; // mt19937 is a good pseudo-random number
  36. // generator.
  37.  
  38. generate(arr1.begin(), arr1.end(), [&] () { return unif(rand_engine); });
  39. generate(arr2.begin(), arr2.end(), [&] () { return unif(rand_engine); });
  40.  
  41. using namespace std::chrono;
  42. using clock = steady_clock;
  43. clock::time_point start = clock::now();
  44. long long s = 0;
  45.  
  46. //for(long long i = 1; i <= 1; ++i) maxArray(&arr1[0], &arr2[0]);
  47. for(long long i = 1; i <= 1; ++i) maxArrayCPPP(arr1, arr2);
  48.  
  49. clock::time_point stop = clock::now();
  50. auto diff = duration_cast<duration<double>>(stop - start).count();
  51.  
  52. cout << s << endl;
  53. cout << "seconds diff: " << diff << endl;
  54.  
  55. for (auto it = arr1.begin(); it != arr1.begin()+10; ++it)
  56. {
  57. cout << ": " << *it << endl;
  58. }
  59.  
  60. return 0;
  61. }
  62.  
  63.  
  64.  
Success #stdin #stdout 0.01s 4248KB
stdin
Standard input is empty
stdout
0
seconds diff: 0.000755161
: 81.3414
: 83.5009
: 96.8868
: 43.7448
: 74.3201
: 88.7315
: 56.8857
: 99.2881
: 99.6461
: 96.7695