fork(2) download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <string>
  4. #include <map>
  5. #include <cstdint>
  6. #include <ctime>
  7. #include <random>
  8. #include <algorithm>
  9. #include <utility>
  10.  
  11. class Elapsed {
  12. std::chrono::high_resolution_clock::time_point firstTime;
  13. std::string mes;
  14. public:
  15. Elapsed(const std::string& s) : mes(s) {
  16. firstTime = std::chrono::high_resolution_clock::now();
  17. }
  18. ~Elapsed() {
  19. std::cout << mes << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now() - firstTime).count() << "ms" << std::endl;
  20. }
  21. };
  22.  
  23. class Heavy {
  24. static const int N = 100000;
  25. double* d;
  26. public:
  27. Heavy() : d(new double[N]) {}
  28. Heavy(const Heavy& h) { // copy constructor
  29. d = new double[N];
  30. std::copy(h.d, h.d + N, d);
  31. }
  32. // Heavy(Heavy&& h) { // move constructor
  33. // d = h.d;
  34. // h.d = nullptr;
  35. // }
  36. ~Heavy() { delete[] d; }
  37. };
  38.  
  39. const int ITER = 100;
  40. const int NSEEDS = 10;
  41.  
  42. int main()
  43. {
  44. typedef std::map<double, Heavy> MDH;
  45. MDH m;
  46.  
  47. std::uint32_t seeds[NSEEDS];
  48. auto t = std::time(nullptr);
  49.  
  50. for (std::size_t i = 0; i < NSEEDS; i++)
  51. if (seeds[i] == 0)
  52. seeds[i] = t * (i + 3) ;
  53.  
  54. std::seed_seq seed_seq(seeds, seeds + NSEEDS);
  55. std::mt19937 twister(seed_seq);
  56. std::uniform_real_distribution<> dist(0.0, 10000.0);
  57.  
  58. {
  59. Elapsed ep("value_type = ");
  60.  
  61. for (int i = 0; i < ITER; i++)
  62. m.insert(MDH::value_type(dist(twister), Heavy()));
  63. }
  64. m.clear();
  65. {
  66. Elapsed ep("std::pair = ");
  67.  
  68. for (int i = 0; i < ITER; i++)
  69. m.insert(std::pair<double, Heavy>(dist(twister), Heavy()));
  70. }
  71. m.clear();
  72. {
  73. Elapsed ep("std::make_pair = ");
  74.  
  75. for (int i = 0; i < ITER; i++)
  76. m.insert(std::make_pair(dist(twister), Heavy()));
  77. }
  78. m.clear();
  79. {
  80. Elapsed ep("std::emplace = ");
  81.  
  82. for (int i = 0; i < ITER; i++)
  83. m.emplace(dist(twister), Heavy());
  84. }
  85. }
  86.  
Success #stdin #stdout 0.6s 3480KB
stdin
Standard input is empty
stdout
value_type = 165ms
std::pair = 165ms
std::make_pair = 164ms
std::emplace = 92ms