fork download
  1. #include <algorithm>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <iterator>
  5. #include <vector>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. vector<string> createArrays() {
  11. vector<string> result(rand() % 100);
  12.  
  13. generate(result.begin(), result.end(), []() {
  14. std::string ret;
  15. for(int i=0; i<32; ++i) {
  16. ret.push_back(rand()%26+'a');
  17. }
  18. return ret;
  19. });
  20.  
  21. return result;
  22. }
  23.  
  24. vector<string> test1(const vector<string>& testValues1, const vector<string>& testValues2) {
  25. vector<string> result(testValues1.size() * testValues2.size());
  26.  
  27. for_each(begin(result), end(result), [&, it = 0U](auto& i) mutable {
  28. i = testValues1[it / testValues2.size()] + testValues2[it % testValues2.size()];
  29. ++it;
  30. });
  31.  
  32. return result;
  33. }
  34.  
  35. vector<string> test2(const vector<string>& testValues1, const vector<string>& testValues2) {
  36. vector<string> result(testValues1.size() * testValues2.size());
  37. auto i = begin(result);
  38.  
  39. std::for_each(cbegin(testValues1), cend(testValues1), [&](const auto& A) { std::for_each(cbegin(testValues2), cend(testValues2), [&](const auto& B) { *i++ = A + B; }); });
  40. return result;
  41. }
  42.  
  43.  
  44. int main() {
  45. clock_t start;
  46. clock_t total1 = 0;
  47. clock_t total2 = 0;
  48. auto first = &test1;
  49. auto second = &test2;
  50.  
  51. srand(static_cast<unsigned int>(time(nullptr)));
  52.  
  53. for (auto i = 0; i < 10; ++i) {
  54. auto testValues1 = createArrays();
  55. auto testValues2 = createArrays();
  56.  
  57. start = clock();
  58. const auto result1 = first(testValues1, testValues2);
  59. total1 += clock() - start;
  60.  
  61. start = clock();
  62. const auto result2 = second(testValues1, testValues2);
  63. total2 += clock() - start;
  64.  
  65. if (!equal(cbegin(result1), cend(result1), cbegin(result2), cend(result2))) {
  66. cout << "MISMATCH\n";
  67. break;
  68. }
  69.  
  70. swap(total1, total2);
  71. swap(first, second);
  72. }
  73. cout << "Test1: " << total1 << endl << "Test2: " << total2 << endl;
  74. }
Success #stdin #stdout 0.01s 3420KB
stdin
Standard input is empty
stdout
Test1: 4469
Test2: 4470