1
0

benchmark.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. for(auto entity: registry.view<int>()) {
  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. view.each([&registry](auto entity, auto &&...) {
  57. if(rand() % 2 == 0) {
  58. registry.destroy(entity);
  59. }
  60. });
  61. }
  62. timer.elapsed();
  63. }
  64. TEST(Benchmark, IterateSingleComponent1M) {
  65. entt::DefaultRegistry registry;
  66. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  67. for(std::uint64_t i = 0; i < 1000000L; i++) {
  68. registry.create<Position>();
  69. }
  70. Timer timer;
  71. registry.view<Position>().each([](auto, auto &) {});
  72. timer.elapsed();
  73. }
  74. TEST(Benchmark, IterateTwoComponents1M) {
  75. entt::DefaultRegistry registry;
  76. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  77. for(std::uint64_t i = 0; i < 1000000L; i++) {
  78. registry.create<Position, Velocity>();
  79. }
  80. Timer timer;
  81. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  82. timer.elapsed();
  83. }
  84. TEST(Benchmark, IterateTwoComponents1MHalf) {
  85. entt::DefaultRegistry registry;
  86. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  87. for(std::uint64_t i = 0; i < 1000000L; i++) {
  88. auto entity = registry.create<Velocity>();
  89. if(i % 2) { registry.assign<Position>(entity); }
  90. }
  91. Timer timer;
  92. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  93. timer.elapsed();
  94. }
  95. TEST(Benchmark, IterateTwoComponents1MOne) {
  96. entt::DefaultRegistry registry;
  97. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  98. for(std::uint64_t i = 0; i < 1000000L; i++) {
  99. auto entity = registry.create<Velocity>();
  100. if(i == 5000000L) { registry.assign<Position>(entity); }
  101. }
  102. Timer timer;
  103. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  104. timer.elapsed();
  105. }
  106. TEST(Benchmark, IterateTwoComponentsPersistent1M) {
  107. entt::DefaultRegistry registry;
  108. registry.prepare<Position, Velocity>();
  109. std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
  110. for(std::uint64_t i = 0; i < 1000000L; i++) {
  111. registry.create<Position, Velocity>();
  112. }
  113. Timer timer;
  114. registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
  115. timer.elapsed();
  116. }
  117. TEST(Benchmark, IterateFiveComponents1M) {
  118. entt::DefaultRegistry registry;
  119. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  120. for(std::uint64_t i = 0; i < 1000000L; i++) {
  121. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  122. }
  123. Timer timer;
  124. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  125. timer.elapsed();
  126. }
  127. TEST(Benchmark, IterateFiveComponents1MHalf) {
  128. entt::DefaultRegistry registry;
  129. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  130. for(std::uint64_t i = 0; i < 1000000L; i++) {
  131. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
  132. if(i % 2) { registry.assign<Position>(entity); }
  133. }
  134. Timer timer;
  135. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  136. timer.elapsed();
  137. }
  138. TEST(Benchmark, IterateFiveComponents1MOne) {
  139. entt::DefaultRegistry registry;
  140. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  141. for(std::uint64_t i = 0; i < 1000000L; i++) {
  142. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
  143. if(i == 5000000L) { registry.assign<Position>(entity); }
  144. }
  145. Timer timer;
  146. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  147. timer.elapsed();
  148. }
  149. TEST(Benchmark, IterateFiveComponentsPersistent1M) {
  150. entt::DefaultRegistry registry;
  151. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  152. std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
  153. for(std::uint64_t i = 0; i < 1000000L; i++) {
  154. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  155. }
  156. Timer timer;
  157. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  158. timer.elapsed();
  159. }
  160. TEST(Benchmark, IterateTenComponents1M) {
  161. entt::DefaultRegistry registry;
  162. std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
  163. for(std::uint64_t i = 0; i < 1000000L; i++) {
  164. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  165. }
  166. Timer timer;
  167. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  168. timer.elapsed();
  169. }
  170. TEST(Benchmark, IterateTenComponents1MHalf) {
  171. entt::DefaultRegistry registry;
  172. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
  173. for(std::uint64_t i = 0; i < 1000000L; i++) {
  174. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  175. if(i % 2) { registry.assign<Position>(entity); }
  176. }
  177. Timer timer;
  178. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  179. timer.elapsed();
  180. }
  181. TEST(Benchmark, IterateTenComponents1MOne) {
  182. entt::DefaultRegistry registry;
  183. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
  184. for(std::uint64_t i = 0; i < 1000000L; i++) {
  185. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  186. if(i == 5000000L) { registry.assign<Position>(entity); }
  187. }
  188. Timer timer;
  189. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  190. timer.elapsed();
  191. }
  192. TEST(Benchmark, IterateTenComponentsPersistent1M) {
  193. entt::DefaultRegistry registry;
  194. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  195. std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
  196. for(std::uint64_t i = 0; i < 1000000L; i++) {
  197. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  198. }
  199. Timer timer;
  200. registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  201. timer.elapsed();
  202. }
  203. TEST(Benchmark, SortSingle) {
  204. entt::DefaultRegistry registry;
  205. std::cout << "Sort 150000 entities, one component" << std::endl;
  206. for(std::uint64_t i = 0; i < 150000L; i++) {
  207. registry.create<Position>({ i, i });
  208. }
  209. Timer timer;
  210. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  211. return lhs.x < rhs.x && lhs.y < rhs.y;
  212. });
  213. timer.elapsed();
  214. }
  215. TEST(Benchmark, SortMulti) {
  216. entt::DefaultRegistry registry;
  217. std::cout << "Sort 150000 entities, two components" << std::endl;
  218. for(std::uint64_t i = 0; i < 150000L; i++) {
  219. registry.create<Position, Velocity>({ i, i }, { i, i });
  220. }
  221. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  222. return lhs.x < rhs.x && lhs.y < rhs.y;
  223. });
  224. Timer timer;
  225. registry.sort<Velocity, Position>();
  226. timer.elapsed();
  227. }