fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <tuple>
  5. #include <chrono>
  6. #include <algorithm>
  7.  
  8. using Entry = std::tuple<std::string, int, int>;
  9.  
  10. int main(int argc, char** argv)
  11. {
  12. const auto t_0 = std::chrono::high_resolution_clock::now();
  13. std::vector<Entry> v;
  14. v.reserve(argc * 1000 * 1000);
  15. for ( std::size_t i = 0; i < v.capacity(); ++i )
  16. {
  17. v.emplace_back(argv[0], 0, 0);
  18. }
  19. const auto t_1 = std::chrono::high_resolution_clock::now();
  20. std::vector<Entry*> pv;
  21. pv.reserve(argc * 1000 * 1000);
  22. for ( std::size_t i = 0; i < pv.capacity(); ++i )
  23. {
  24. pv.push_back(new std::tuple<std::string, int, int>(argv[0], 0, 0));
  25. }
  26. const auto t_2 = std::chrono::high_resolution_clock::now();
  27. auto rv = [&]()
  28. {
  29. std::sort(v.begin(), v.end(), [](const Entry& a, const Entry& b) { return a < b; });
  30. return std::get<1>(v.back());
  31. }();
  32. const auto t_3 = std::chrono::high_resolution_clock::now();
  33. auto rpv = [&]()
  34. {
  35. std::sort(pv.begin(), pv.end(), [](const Entry* a, const Entry* b) { return *a < *b; });
  36. return std::get<1>(*pv.back());
  37. }();
  38. const auto t_4 = std::chrono::high_resolution_clock::now();
  39. for ( auto& e : pv )
  40. {
  41. delete e;
  42. }
  43. const auto t_5 = std::chrono::high_resolution_clock::now();
  44. std::cout << (t_1 - t_0).count() << " for construction of v\n"
  45. << (t_3 - t_2).count() << " for sorting v\n"
  46. << (t_3 - t_2).count() + (t_1 - t_0).count() << " in total for v\n"
  47. << (t_2 - t_1).count() << " for construction of pv\n"
  48. << (t_4 - t_3).count() << " for sorting pv\n"
  49. << (t_5 - t_4).count() << " for destruction of pv\n"
  50. << (t_2 - t_1).count() + (t_4 - t_3).count() + (t_5 - t_4).count() << " in total for pv\n";
  51. return rpv - rv;
  52. }
Success #stdin #stdout 2.31s 65664KB
stdin
Standard input is empty
stdout
136502146 for construction of v
944132760 for sorting v
1080634906 in total for v
214762852 for construction of pv
888223546 for sorting pv
85994736 for destruction of pv
1188981134 in total for pv