fork(2) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <chrono>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. struct Copyable
  8. {
  9. static const int SIZE = 10000;
  10.  
  11. Copyable()
  12. {
  13. data = new int[SIZE];
  14. for(int i = 0; i < sizeof(data); ++i)
  15. data[i] = rand();
  16. }
  17. virtual ~Copyable()
  18. {
  19. delete[] data;
  20. }
  21.  
  22. Copyable(const Copyable& other)
  23. {
  24. data = new int[SIZE];
  25. std::copy(other.data, other.data + SIZE, data);
  26. }
  27. Copyable& operator=(const Copyable& other)
  28. {
  29. if(this == &other) return *this;
  30. delete[] data;
  31. data = new int[SIZE];
  32. std::copy(other.data, other.data + SIZE, data);
  33. return *this;
  34. }
  35. protected:
  36. int *data;
  37. };
  38.  
  39. struct Movable
  40. {
  41. static const int SIZE = 10000;
  42.  
  43. Movable()
  44. {
  45. data = new int[SIZE];
  46. for(int i = 0; i < sizeof(data); ++i)
  47. data[i] = rand();
  48. }
  49. virtual ~Movable()
  50. {
  51. delete[] data;
  52. }
  53.  
  54. Movable(const Movable& other)
  55. {
  56. data = new int[SIZE];
  57. std::copy(other.data, other.data + SIZE, data);
  58. }
  59. Movable& operator=(const Movable& other)
  60. {
  61. if(this == &other) return *this;
  62. delete[] data;
  63. data = new int[SIZE];
  64. std::copy(other.data, other.data + SIZE, data);
  65. return *this;
  66. }
  67.  
  68. Movable(Movable&& other)
  69. {
  70. data = other.data;
  71. other.data = 0;
  72. }
  73. Movable& operator=(Movable&& other)
  74. {
  75. if(this == &other) return *this;
  76. int *p = data;
  77. data = other.data;
  78. other.data = p;
  79. return *this;
  80. }
  81. protected:
  82. int *data;
  83. };
  84.  
  85. template <class T>
  86. T generate()
  87. {
  88. return T();
  89. }
  90.  
  91. template <class T>
  92. void swap_copy(T& a, T& b)
  93. {
  94. T t(a);
  95. a = b;
  96. b = t;
  97. }
  98.  
  99. template <class T>
  100. void swap_move(T& a, T& b)
  101. {
  102. T t(std::move(a));
  103. a = std::move(b);
  104. b = std::move(t);
  105. }
  106.  
  107. template <class Function>
  108. void measure(Function fun, const std::string& msg)
  109. {
  110. std::chrono::high_resolution_clock::time_point start, end;
  111. start = std::chrono::high_resolution_clock::now();
  112.  
  113. fun();
  114.  
  115. end = std::chrono::high_resolution_clock::now();
  116. auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
  117. std::cout << msg << ": " << duration << "ms" << std::endl;
  118. }
  119.  
  120. void test_copy_return()
  121. {
  122. Copyable copy_obj;
  123. for(int i = 0; i < 100000; ++i)
  124. copy_obj = generate<Copyable>();
  125. }
  126.  
  127. void test_move_return()
  128. {
  129. Movable move_obj;
  130. for(int i = 0; i < 100000; ++i)
  131. move_obj = generate<Movable>();
  132. }
  133.  
  134. void test_copy_swap()
  135. {
  136. Copyable copy_obj_a, copy_obj_b;
  137. for(int i = 0; i < 100000; ++i)
  138. swap_copy(copy_obj_a, copy_obj_b);
  139. }
  140.  
  141. void test_move_swap()
  142. {
  143. Movable move_obj_a, move_obj_b;
  144. for(int i = 0; i < 100000; ++i)
  145. swap_move(move_obj_a, move_obj_b);
  146. }
  147.  
  148. int main()
  149. {
  150. srand (time(NULL));
  151.  
  152. measure(test_copy_return, "Copy return");
  153. measure(test_move_return, "Move return");
  154. measure(test_copy_swap, "Copy swap");
  155. measure(test_move_swap, "Move swap");
  156.  
  157. return 0;
  158. }
  159.  
  160. // > g++ move_rvo_test.cpp -std=c++11 -o prog.exe
  161. // > prog.exe
  162. // Copy return: 224ms
  163. // Move return: 25ms
  164. // Copy swap: 524ms
  165. // Move swap: 2ms
  166.  
  167. // > g++ move_rvo_test.cpp -std=c++11 -o prog.exe -fno-elide-constructors
  168. // > prog.exe
  169. // Copy return: 500ms
  170. // Move return: 33ms
  171. // Copy swap: 522ms
  172. // Move swap: 1ms
Success #stdin #stdout 4.44s 3460KB
stdin
Standard input is empty
stdout
Copy return: 1671ms
Move return: 19ms
Copy swap: 2778ms
Move swap: 0ms