benchmark.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 10000000 entities" << std::endl;
  30. Timer timer;
  31. for(std::uint64_t i = 0; i < 10000000L; 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 10000000 entities" << std::endl;
  40. for(std::uint64_t i = 0; i < 10000000L; 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, IterateSingleComponent10M) {
  67. entt::DefaultRegistry registry;
  68. std::cout << "Iterating over 10000000 entities, one component" << std::endl;
  69. for(std::uint64_t i = 0; i < 10000000L; i++) {
  70. registry.create<Position>();
  71. }
  72. Timer timer;
  73. registry.view<Position>().each([](auto, auto &) {});
  74. timer.elapsed();
  75. }
  76. TEST(Benchmark, IterateTwoComponents10M) {
  77. entt::DefaultRegistry registry;
  78. std::cout << "Iterating over 10000000 entities, two components" << std::endl;
  79. for(std::uint64_t i = 0; i < 10000000L; 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, IterateTwoComponents10MHalf) {
  87. entt::DefaultRegistry registry;
  88. std::cout << "Iterating over 10000000 entities, two components, half of the entities have all the components" << std::endl;
  89. for(std::uint64_t i = 0; i < 10000000L; 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, IterateTwoComponents10MOne) {
  98. entt::DefaultRegistry registry;
  99. std::cout << "Iterating over 10000000 entities, two components, only one entity has all the components" << std::endl;
  100. for(std::uint64_t i = 0; i < 10000000L; 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, IterateTwoComponentsPersistent10M) {
  109. entt::DefaultRegistry registry;
  110. registry.prepare<Position, Velocity>();
  111. std::cout << "Iterating over 10000000 entities, two components, persistent view" << std::endl;
  112. for(std::uint64_t i = 0; i < 10000000L; 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, IterateTwoComponentsPersistent10MHalf) {
  120. entt::DefaultRegistry registry;
  121. registry.prepare<Position, Velocity>();
  122. std::cout << "Iterating over 10000000 entities, two components, persistent view, half of the entities have all the components" << std::endl;
  123. for(std::uint64_t i = 0; i < 10000000L; i++) {
  124. auto entity = registry.create<Velocity>();
  125. if(i % 2) { registry.assign<Position>(entity); }
  126. }
  127. Timer timer;
  128. registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
  129. timer.elapsed();
  130. }
  131. TEST(Benchmark, IterateTwoComponentsPersistent10MOne) {
  132. entt::DefaultRegistry registry;
  133. registry.prepare<Position, Velocity>();
  134. std::cout << "Iterating over 10000000 entities, two components, persistent view, only one entity has all the components" << std::endl;
  135. for(std::uint64_t i = 0; i < 10000000L; i++) {
  136. auto entity = registry.create<Velocity>();
  137. if(i == 5000000L) { registry.assign<Position>(entity); }
  138. }
  139. Timer timer;
  140. registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
  141. timer.elapsed();
  142. }
  143. TEST(Benchmark, IterateFiveComponents10M) {
  144. entt::DefaultRegistry registry;
  145. std::cout << "Iterating over 10000000 entities, five components" << std::endl;
  146. for(std::uint64_t i = 0; i < 10000000L; i++) {
  147. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  148. }
  149. Timer timer;
  150. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  151. timer.elapsed();
  152. }
  153. TEST(Benchmark, IterateTenComponents10M) {
  154. entt::DefaultRegistry registry;
  155. std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
  156. for(std::uint64_t i = 0; i < 10000000L; i++) {
  157. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  158. }
  159. Timer timer;
  160. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  161. timer.elapsed();
  162. }
  163. TEST(Benchmark, IterateTenComponents10MHalf) {
  164. entt::DefaultRegistry registry;
  165. std::cout << "Iterating over 10000000 entities, ten components, half of the entities have all the components" << std::endl;
  166. for(std::uint64_t i = 0; i < 10000000L; i++) {
  167. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  168. if(i % 2) { registry.assign<Position>(entity); }
  169. }
  170. Timer timer;
  171. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  172. timer.elapsed();
  173. }
  174. TEST(Benchmark, IterateTenComponents10MOne) {
  175. entt::DefaultRegistry registry;
  176. std::cout << "Iterating over 10000000 entities, ten components, only one entity has all the components" << std::endl;
  177. for(std::uint64_t i = 0; i < 10000000L; i++) {
  178. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  179. if(i == 5000000L) { registry.assign<Position>(entity); }
  180. }
  181. Timer timer;
  182. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  183. timer.elapsed();
  184. }
  185. TEST(Benchmark, IterateFiveComponentsPersistent10M) {
  186. entt::DefaultRegistry registry;
  187. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  188. std::cout << "Iterating over 10000000 entities, five components, persistent view" << std::endl;
  189. for(std::uint64_t i = 0; i < 10000000L; i++) {
  190. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  191. }
  192. Timer timer;
  193. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  194. timer.elapsed();
  195. }
  196. TEST(Benchmark, IterateTenComponentsPersistent10M) {
  197. entt::DefaultRegistry registry;
  198. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  199. std::cout << "Iterating over 10000000 entities, ten components, persistent view" << std::endl;
  200. for(std::uint64_t i = 0; i < 10000000L; i++) {
  201. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  202. }
  203. Timer timer;
  204. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  205. timer.elapsed();
  206. }
  207. TEST(Benchmark, IterateTenComponentsPersistent10MHalf) {
  208. entt::DefaultRegistry registry;
  209. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  210. std::cout << "Iterating over 10000000 entities, ten components, persistent view, half of the entities have all the components" << std::endl;
  211. for(std::uint64_t i = 0; i < 10000000L; i++) {
  212. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  213. if(i % 2) { registry.assign<Position>(entity); }
  214. }
  215. Timer timer;
  216. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  217. timer.elapsed();
  218. }
  219. TEST(Benchmark, IterateTenComponentsPersistent10MOne) {
  220. entt::DefaultRegistry registry;
  221. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  222. std::cout << "Iterating over 10000000 entities, ten components, persistent view, only one entity has all the components" << std::endl;
  223. for(std::uint64_t i = 0; i < 10000000L; i++) {
  224. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  225. if(i == 5000000L) { registry.assign<Position>(entity); }
  226. }
  227. Timer timer;
  228. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  229. timer.elapsed();
  230. }
  231. TEST(Benchmark, SortSingle) {
  232. entt::DefaultRegistry registry;
  233. std::vector<entt::DefaultRegistry::entity_type> entities{};
  234. std::cout << "Sort 150000 entities, one component" << std::endl;
  235. for(std::uint64_t i = 0; i < 150000L; i++) {
  236. auto entity = registry.create<Position>({ i, i });
  237. entities.push_back(entity);
  238. }
  239. Timer timer;
  240. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  241. return lhs.x < rhs.x && lhs.y < rhs.y;
  242. });
  243. timer.elapsed();
  244. }
  245. TEST(Benchmark, SortMulti) {
  246. entt::DefaultRegistry registry;
  247. std::vector<entt::DefaultRegistry::entity_type> entities{};
  248. std::cout << "Sort 150000 entities, two components" << std::endl;
  249. for(std::uint64_t i = 0; i < 150000L; i++) {
  250. auto entity = registry.create<Position, Velocity>({ i, i }, { i, i });
  251. entities.push_back(entity);
  252. }
  253. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  254. return lhs.x < rhs.x && lhs.y < rhs.y;
  255. });
  256. Timer timer;
  257. registry.sort<Velocity, Position>();
  258. timer.elapsed();
  259. }