benchmark.cpp 9.4 KB

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