fork download
  1. #include <set>
  2. #include <chrono>
  3. #include <iostream>
  4.  
  5. using Time = std::chrono::high_resolution_clock;
  6. using ms = std::chrono::milliseconds;
  7.  
  8. int main()
  9. {
  10. std::set<int> initialSet;
  11. for (int i = 0; i < 5000000; ++i) {
  12. initialSet.insert(i);
  13. }
  14.  
  15. std::set<int> setFilledWithForInsert;
  16. {
  17. const auto start = Time::now();
  18. for (const int value: initialSet) {
  19. setFilledWithForInsert.insert(value);
  20. }
  21. std::cout << "Insert: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n";
  22. }
  23.  
  24. std::set<int> setFilledWithForEmplaceHint;
  25. {
  26. const auto start = Time::now();
  27. auto it = setFilledWithForEmplaceHint.cend();
  28. for (const int value: initialSet) {
  29. it = setFilledWithForEmplaceHint.emplace_hint(it, value);
  30. }
  31. std::cout << "EmplaceHint: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n";
  32. }
  33.  
  34. std::set<int> setFilledWithForInsertHint;
  35. {
  36. const auto start = std::chrono::system_clock::now();
  37. auto it = setFilledWithForEmplaceHint.cend();
  38. for (const int value: initialSet) {
  39. it = setFilledWithForInsertHint.insert(it, value);
  40. }
  41. std::cout << "InsertHint: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n";
  42. }
  43.  
  44. std::set<int> setFilledWithIterators;
  45. {
  46. const auto start = std::chrono::system_clock::now();
  47. setFilledWithIterators.insert(initialSet.cbegin(), initialSet.cend());
  48. std::cout << "InsertIterators: " << std::chrono::duration_cast<ms>(Time::now() - start).count() << "\n";
  49. }
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 3.47s 1187328KB
stdin
Standard input is empty
stdout
Insert: 1065
EmplaceHint: 256
InsertHint: 281
InsertIterators: 273