benchmark.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <chrono>
  5. #include <gtest/gtest.h>
  6. #include <entt/entity/registry.hpp>
  7. struct Position {
  8. std::uint64_t x;
  9. std::uint64_t y;
  10. };
  11. struct Velocity {
  12. std::uint64_t x;
  13. std::uint64_t y;
  14. };
  15. template<std::size_t>
  16. struct Comp { int x; };
  17. struct Timer final {
  18. Timer(): start{std::chrono::system_clock::now()} {}
  19. void elapsed() {
  20. auto now = std::chrono::system_clock::now();
  21. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  22. }
  23. private:
  24. std::chrono::time_point<std::chrono::system_clock> start;
  25. };
  26. TEST(Benchmark, Construct) {
  27. entt::DefaultRegistry registry;
  28. std::cout << "Constructing 1000000 entities" << std::endl;
  29. Timer timer;
  30. for(std::uint64_t i = 0; i < 1000000L; i++) {
  31. registry.create();
  32. }
  33. timer.elapsed();
  34. }
  35. TEST(Benchmark, Destroy) {
  36. entt::DefaultRegistry registry;
  37. std::cout << "Destroying 1000000 entities" << std::endl;
  38. for(std::uint64_t i = 0; i < 1000000L; i++) {
  39. registry.create();
  40. }
  41. Timer timer;
  42. registry.each([&registry](auto entity) {
  43. registry.destroy(entity);
  44. });
  45. timer.elapsed();
  46. }
  47. TEST(Benchmark, IterateCreateDeleteSingleComponent) {
  48. entt::DefaultRegistry registry;
  49. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  50. Timer timer;
  51. auto view = registry.view<Position>();
  52. for(int i = 0; i < 10000; i++) {
  53. for(int j = 0; j < 10000; j++) {
  54. registry.create<Position>();
  55. }
  56. for(auto entity: view) {
  57. const auto &position = view.get(entity);
  58. (void)position;
  59. if(rand() % 2 == 0) {
  60. registry.destroy(entity);
  61. }
  62. };
  63. }
  64. timer.elapsed();
  65. }
  66. TEST(Benchmark, IterateSingleComponent1M) {
  67. entt::DefaultRegistry registry;
  68. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  69. for(std::uint64_t i = 0; i < 1000000L; i++) {
  70. registry.create<Position>();
  71. }
  72. Timer timer;
  73. registry.view<Position>().each([](auto, auto &) {});
  74. timer.elapsed();
  75. }
  76. TEST(Benchmark, IterateSingleComponentRaw1M) {
  77. entt::DefaultRegistry registry;
  78. std::cout << "Iterating over 1000000 entities, one component, raw view" << std::endl;
  79. for(std::uint64_t i = 0; i < 1000000L; i++) {
  80. registry.create<Position>();
  81. }
  82. Timer timer;
  83. for(auto &&component: registry.raw<Position>()) {
  84. (void)component;
  85. }
  86. timer.elapsed();
  87. }
  88. TEST(Benchmark, IterateTwoComponents1M) {
  89. entt::DefaultRegistry registry;
  90. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  91. for(std::uint64_t i = 0; i < 1000000L; i++) {
  92. registry.create<Position, Velocity>();
  93. }
  94. Timer timer;
  95. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  96. timer.elapsed();
  97. }
  98. TEST(Benchmark, IterateTwoComponents1MHalf) {
  99. entt::DefaultRegistry registry;
  100. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  101. for(std::uint64_t i = 0; i < 1000000L; i++) {
  102. auto entity = registry.create<Velocity>();
  103. if(i % 2) { registry.assign<Position>(entity); }
  104. }
  105. Timer timer;
  106. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  107. timer.elapsed();
  108. }
  109. TEST(Benchmark, IterateTwoComponents1MOne) {
  110. entt::DefaultRegistry registry;
  111. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  112. for(std::uint64_t i = 0; i < 1000000L; i++) {
  113. auto entity = registry.create<Velocity>();
  114. if(i == 5000000L) { registry.assign<Position>(entity); }
  115. }
  116. Timer timer;
  117. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  118. timer.elapsed();
  119. }
  120. TEST(Benchmark, IterateTwoComponentsPersistent1M) {
  121. entt::DefaultRegistry registry;
  122. registry.prepare<Position, Velocity>();
  123. std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
  124. for(std::uint64_t i = 0; i < 1000000L; i++) {
  125. registry.create<Position, Velocity>();
  126. }
  127. Timer timer;
  128. registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
  129. timer.elapsed();
  130. }
  131. TEST(Benchmark, IterateFiveComponents1M) {
  132. entt::DefaultRegistry registry;
  133. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  134. for(std::uint64_t i = 0; i < 1000000L; i++) {
  135. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  136. }
  137. Timer timer;
  138. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  139. timer.elapsed();
  140. }
  141. TEST(Benchmark, IterateFiveComponents1MHalf) {
  142. entt::DefaultRegistry registry;
  143. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  144. for(std::uint64_t i = 0; i < 1000000L; i++) {
  145. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
  146. if(i % 2) { registry.assign<Position>(entity); }
  147. }
  148. Timer timer;
  149. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  150. timer.elapsed();
  151. }
  152. TEST(Benchmark, IterateFiveComponents1MOne) {
  153. entt::DefaultRegistry registry;
  154. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  155. for(std::uint64_t i = 0; i < 1000000L; i++) {
  156. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
  157. if(i == 5000000L) { registry.assign<Position>(entity); }
  158. }
  159. Timer timer;
  160. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  161. timer.elapsed();
  162. }
  163. TEST(Benchmark, IterateFiveComponentsPersistent1M) {
  164. entt::DefaultRegistry registry;
  165. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  166. std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
  167. for(std::uint64_t i = 0; i < 1000000L; i++) {
  168. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  169. }
  170. Timer timer;
  171. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  172. timer.elapsed();
  173. }
  174. TEST(Benchmark, IterateTenComponents1M) {
  175. entt::DefaultRegistry registry;
  176. std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
  177. for(std::uint64_t i = 0; i < 1000000L; i++) {
  178. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  179. }
  180. Timer timer;
  181. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  182. timer.elapsed();
  183. }
  184. TEST(Benchmark, IterateTenComponents1MHalf) {
  185. entt::DefaultRegistry registry;
  186. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
  187. for(std::uint64_t i = 0; i < 1000000L; i++) {
  188. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  189. if(i % 2) { registry.assign<Position>(entity); }
  190. }
  191. Timer timer;
  192. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  193. timer.elapsed();
  194. }
  195. TEST(Benchmark, IterateTenComponents1MOne) {
  196. entt::DefaultRegistry registry;
  197. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
  198. for(std::uint64_t i = 0; i < 1000000L; i++) {
  199. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  200. if(i == 5000000L) { registry.assign<Position>(entity); }
  201. }
  202. Timer timer;
  203. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  204. timer.elapsed();
  205. }
  206. TEST(Benchmark, IterateTenComponentsPersistent1M) {
  207. entt::DefaultRegistry registry;
  208. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  209. std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
  210. for(std::uint64_t i = 0; i < 1000000L; i++) {
  211. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  212. }
  213. Timer timer;
  214. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  215. timer.elapsed();
  216. }
  217. TEST(Benchmark, SortSingle) {
  218. entt::DefaultRegistry registry;
  219. std::cout << "Sort 150000 entities, one component" << std::endl;
  220. for(std::uint64_t i = 0; i < 150000L; i++) {
  221. registry.create<Position>({ i, i });
  222. }
  223. Timer timer;
  224. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  225. return lhs.x < rhs.x && lhs.y < rhs.y;
  226. });
  227. timer.elapsed();
  228. }
  229. TEST(Benchmark, SortMulti) {
  230. entt::DefaultRegistry registry;
  231. std::cout << "Sort 150000 entities, two components" << std::endl;
  232. for(std::uint64_t i = 0; i < 150000L; i++) {
  233. registry.create<Position, Velocity>({ i, i }, { i, i });
  234. }
  235. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  236. return lhs.x < rhs.x && lhs.y < rhs.y;
  237. });
  238. Timer timer;
  239. registry.sort<Velocity, Position>();
  240. timer.elapsed();
  241. }