fork download
  1. #include <chrono>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. int main(int argc, char**)
  6. {
  7. const auto t_0 = std::chrono::high_resolution_clock::now();
  8. std::vector<int> v(argc * 1000 * 1000, 0);
  9. const auto t_1 = std::chrono::high_resolution_clock::now();
  10. std::vector<int*> pv(argc * 1000 * 1000, nullptr);
  11. for ( auto& e : pv )
  12. {
  13. e = new int(0);
  14. }
  15. const auto t_2 = std::chrono::high_resolution_clock::now();
  16. auto rv = [&]()
  17. {
  18. int sum = 0;
  19. for ( const auto& e : v )
  20. {
  21. sum += e;
  22. }
  23. return sum;
  24. }();
  25. const auto t_3 = std::chrono::high_resolution_clock::now();
  26. auto rpv = [&]()
  27. {
  28. int sum = 0;
  29. for ( const auto& e : pv )
  30. {
  31. sum += *e;
  32. }
  33. return sum;
  34. }();
  35. const auto t_4 = std::chrono::high_resolution_clock::now();
  36. for ( auto& e : pv )
  37. {
  38. delete e;
  39. }
  40. const auto t_5 = std::chrono::high_resolution_clock::now();
  41. std::cout << (t_1 - t_0).count() << " for construction of v\n"
  42. << (t_3 - t_2).count() << " for sum of v\n"
  43. << (t_3 - t_2).count() + (t_1 - t_0).count() << " in total for v\n"
  44. << (t_2 - t_1).count() << " for construction of pv\n"
  45. << (t_4 - t_3).count() << " for sum of pv\n"
  46. << (t_5 - t_4).count() << " for destruction of pv\n"
  47. << (t_2 - t_1).count() + (t_4 - t_3).count() + (t_5 - t_4).count() << " in total for pv\n";
  48. return rpv - rv;
  49. }
Success #stdin #stdout 0.1s 18848KB
stdin
Standard input is empty
stdout
3697674 for construction of v
1385545 for sum of v
5083219 in total for v
64752873 for construction of pv
4700794 for sum of pv
32847649 for destruction of pv
102301316 in total for pv