benchmark.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. #include <gtest/gtest.h>
  2. #include <registry.hpp>
  3. #include <functional>
  4. #include <iostream>
  5. #include <cstddef>
  6. #include <chrono>
  7. #include <vector>
  8. struct Position {
  9. uint64_t x;
  10. uint64_t y;
  11. };
  12. struct Velocity {
  13. uint64_t x;
  14. 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(DefaultRegistry, Construct) {
  28. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  29. registry_type registry;
  30. std::cout << "Constructing 10000000 entities" << std::endl;
  31. Timer timer;
  32. for (uint64_t i = 0; i < 10000000L; i++) {
  33. registry.create();
  34. }
  35. timer.elapsed();
  36. registry.reset();
  37. }
  38. TEST(DefaultRegistry, Destroy) {
  39. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  40. registry_type registry;
  41. std::vector<registry_type::entity_type> entities{};
  42. std::cout << "Destroying 10000000 entities" << std::endl;
  43. for (uint64_t i = 0; i < 10000000L; i++) {
  44. entities.push_back(registry.create());
  45. }
  46. Timer timer;
  47. for (auto entity: entities) {
  48. registry.destroy(entity);
  49. }
  50. timer.elapsed();
  51. }
  52. TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
  53. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  54. registry_type registry;
  55. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  56. Timer timer;
  57. for(int i = 0; i < 10000; i++) {
  58. for(int j = 0; j < 10000; j++) {
  59. registry.create<Position>();
  60. }
  61. auto view = registry.view<Position>();
  62. for(auto entity: view) {
  63. if(rand() % 2 == 0) {
  64. registry.destroy(entity);
  65. }
  66. }
  67. }
  68. timer.elapsed();
  69. registry.reset();
  70. }
  71. TEST(DefaultRegistry, IterateSingleComponent10M) {
  72. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  73. registry_type registry;
  74. std::cout << "Iterating over 10000000 entities, one component" << std::endl;
  75. for (uint64_t i = 0; i < 10000000L; i++) {
  76. registry.create<Position>();
  77. }
  78. Timer timer;
  79. auto view = registry.view<Position>();
  80. for(auto entity: view) {
  81. auto &position = registry.get<Position>(entity);
  82. (void)position;
  83. }
  84. timer.elapsed();
  85. registry.reset();
  86. }
  87. TEST(DefaultRegistry, IterateTwoComponents10M) {
  88. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  89. registry_type registry;
  90. std::cout << "Iterating over 10000000 entities, two components" << std::endl;
  91. for (uint64_t i = 0; i < 10000000L; i++) {
  92. registry.create<Position, Velocity>();
  93. }
  94. Timer timer;
  95. auto view = registry.view<Position, Velocity>();
  96. for(auto entity: view) {
  97. auto &position = registry.get<Position>(entity);
  98. auto &velocity = registry.get<Velocity>(entity);
  99. (void)position;
  100. (void)velocity;
  101. }
  102. timer.elapsed();
  103. registry.reset();
  104. }
  105. TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
  106. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  107. registry_type registry;
  108. std::cout << "Iterating over 10000000 entities, two components, half of the entities have all the components" << std::endl;
  109. for (uint64_t i = 0; i < 10000000L; i++) {
  110. auto entity = registry.create<Velocity>();
  111. if(i % 2) { registry.assign<Position>(entity); }
  112. }
  113. Timer timer;
  114. auto view = registry.view<Position, Velocity>();
  115. for(auto entity: view) {
  116. auto &position = registry.get<Position>(entity);
  117. auto &velocity = registry.get<Velocity>(entity);
  118. (void)position;
  119. (void)velocity;
  120. }
  121. timer.elapsed();
  122. registry.reset();
  123. }
  124. TEST(DefaultRegistry, IterateTwoComponents10MOne) {
  125. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  126. registry_type registry;
  127. std::cout << "Iterating over 10000000 entities, two components, only one entity has all the components" << std::endl;
  128. for (uint64_t i = 0; i < 10000000L; i++) {
  129. auto entity = registry.create<Velocity>();
  130. if(i == 5000000L) { registry.assign<Position>(entity); }
  131. }
  132. Timer timer;
  133. auto view = registry.view<Position, Velocity>();
  134. for(auto entity: view) {
  135. auto &position = registry.get<Position>(entity);
  136. auto &velocity = registry.get<Velocity>(entity);
  137. (void)position;
  138. (void)velocity;
  139. }
  140. timer.elapsed();
  141. registry.reset();
  142. }
  143. TEST(DefaultRegistry, IterateSingleComponent50M) {
  144. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  145. registry_type registry;
  146. std::cout << "Iterating over 50000000 entities, one component" << std::endl;
  147. for (uint64_t i = 0; i < 50000000L; i++) {
  148. registry.create<Position>();
  149. }
  150. Timer timer;
  151. auto view = registry.view<Position>();
  152. for(auto entity: view) {
  153. auto &position = registry.get<Position>(entity);
  154. (void)position;
  155. }
  156. timer.elapsed();
  157. registry.reset();
  158. }
  159. TEST(DefaultRegistry, IterateTwoComponents50M) {
  160. using registry_type = entt::DefaultRegistry<Position, Velocity>;
  161. registry_type registry;
  162. std::cout << "Iterating over 50000000 entities, two components" << std::endl;
  163. for (uint64_t i = 0; i < 50000000L; i++) {
  164. registry.create<Position, Velocity>();
  165. }
  166. Timer timer;
  167. auto view = registry.view<Position, Velocity>();
  168. for(auto entity: view) {
  169. auto &position = registry.get<Position>(entity);
  170. auto &velocity = registry.get<Velocity>(entity);
  171. (void)position;
  172. (void)velocity;
  173. }
  174. timer.elapsed();
  175. registry.reset();
  176. }
  177. TEST(DefaultRegistry, IterateFiveComponents10M) {
  178. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>;
  179. registry_type registry;
  180. std::cout << "Iterating over 10000000 entities, five components" << std::endl;
  181. for (uint64_t i = 0; i < 10000000L; i++) {
  182. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  183. }
  184. Timer timer;
  185. auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  186. for(auto entity: view) {
  187. auto &position = registry.get<Position>(entity);
  188. auto &velocity = registry.get<Velocity>(entity);
  189. auto &comp1 = registry.get<Comp<1>>(entity);
  190. auto &comp2 = registry.get<Comp<2>>(entity);
  191. auto &comp3 = registry.get<Comp<3>>(entity);
  192. (void)position;
  193. (void)velocity;
  194. (void)comp1;
  195. (void)comp2;
  196. (void)comp3;
  197. }
  198. timer.elapsed();
  199. registry.reset();
  200. }
  201. TEST(DefaultRegistry, IterateTenComponents10M) {
  202. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>;
  203. registry_type registry;
  204. std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
  205. for (uint64_t i = 0; i < 10000000L; i++) {
  206. registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  207. }
  208. Timer timer;
  209. auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  210. for(auto entity: view) {
  211. auto &position = registry.get<Position>(entity);
  212. auto &velocity = registry.get<Velocity>(entity);
  213. auto &comp1 = registry.get<Comp<1>>(entity);
  214. auto &comp2 = registry.get<Comp<2>>(entity);
  215. auto &comp3 = registry.get<Comp<3>>(entity);
  216. auto &comp4 = registry.get<Comp<4>>(entity);
  217. auto &comp5 = registry.get<Comp<5>>(entity);
  218. auto &comp6 = registry.get<Comp<6>>(entity);
  219. auto &comp7 = registry.get<Comp<7>>(entity);
  220. auto &comp8 = registry.get<Comp<8>>(entity);
  221. (void)position;
  222. (void)velocity;
  223. (void)comp1;
  224. (void)comp2;
  225. (void)comp3;
  226. (void)comp4;
  227. (void)comp5;
  228. (void)comp6;
  229. (void)comp7;
  230. (void)comp8;
  231. }
  232. timer.elapsed();
  233. registry.reset();
  234. }
  235. TEST(DefaultRegistry, IterateTenComponents10MHalf) {
  236. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>;
  237. registry_type registry;
  238. std::cout << "Iterating over 10000000 entities, ten components, half of the entities have all the components" << std::endl;
  239. for (uint64_t i = 0; i < 10000000L; i++) {
  240. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  241. if(i % 2) { registry.assign<Position>(entity); }
  242. }
  243. Timer timer;
  244. auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  245. for(auto entity: view) {
  246. auto &position = registry.get<Position>(entity);
  247. auto &velocity = registry.get<Velocity>(entity);
  248. auto &comp1 = registry.get<Comp<1>>(entity);
  249. auto &comp2 = registry.get<Comp<2>>(entity);
  250. auto &comp3 = registry.get<Comp<3>>(entity);
  251. auto &comp4 = registry.get<Comp<4>>(entity);
  252. auto &comp5 = registry.get<Comp<5>>(entity);
  253. auto &comp6 = registry.get<Comp<6>>(entity);
  254. auto &comp7 = registry.get<Comp<7>>(entity);
  255. auto &comp8 = registry.get<Comp<8>>(entity);
  256. (void)position;
  257. (void)velocity;
  258. (void)comp1;
  259. (void)comp2;
  260. (void)comp3;
  261. (void)comp4;
  262. (void)comp5;
  263. (void)comp6;
  264. (void)comp7;
  265. (void)comp8;
  266. }
  267. timer.elapsed();
  268. registry.reset();
  269. }
  270. TEST(DefaultRegistry, IterateTenComponents10MOne) {
  271. using registry_type = entt::DefaultRegistry<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>;
  272. registry_type registry;
  273. std::cout << "Iterating over 10000000 entities, ten components, only one entity has all the components" << std::endl;
  274. for (uint64_t i = 0; i < 10000000L; i++) {
  275. auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  276. if(i == 5000000L) { registry.assign<Position>(entity); }
  277. }
  278. Timer timer;
  279. auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  280. for(auto entity: view) {
  281. auto &position = registry.get<Position>(entity);
  282. auto &velocity = registry.get<Velocity>(entity);
  283. auto &comp1 = registry.get<Comp<1>>(entity);
  284. auto &comp2 = registry.get<Comp<2>>(entity);
  285. auto &comp3 = registry.get<Comp<3>>(entity);
  286. auto &comp4 = registry.get<Comp<4>>(entity);
  287. auto &comp5 = registry.get<Comp<5>>(entity);
  288. auto &comp6 = registry.get<Comp<6>>(entity);
  289. auto &comp7 = registry.get<Comp<7>>(entity);
  290. auto &comp8 = registry.get<Comp<8>>(entity);
  291. (void)position;
  292. (void)velocity;
  293. (void)comp1;
  294. (void)comp2;
  295. (void)comp3;
  296. (void)comp4;
  297. (void)comp5;
  298. (void)comp6;
  299. (void)comp7;
  300. (void)comp8;
  301. }
  302. timer.elapsed();
  303. registry.reset();
  304. }