benchmark.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. const auto entity = registry.create();
  55. registry.assign<Position>(entity);
  56. }
  57. view.each([&](auto entity, auto &position) {
  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. const auto entity = registry.create();
  71. registry.assign<Position>(entity);
  72. }
  73. Timer timer;
  74. registry.view<Position>().each([](auto, auto &) {});
  75. timer.elapsed();
  76. }
  77. TEST(Benchmark, IterateSingleComponentRaw1M) {
  78. entt::DefaultRegistry registry;
  79. std::cout << "Iterating over 1000000 entities, one component, raw view" << std::endl;
  80. for(std::uint64_t i = 0; i < 1000000L; i++) {
  81. const auto entity = registry.create();
  82. registry.assign<Position>(entity);
  83. }
  84. Timer timer;
  85. for(auto &&component: registry.view<Position>(entt::raw_t{})) {
  86. (void)component;
  87. }
  88. timer.elapsed();
  89. }
  90. TEST(Benchmark, IterateTwoComponents1M) {
  91. entt::DefaultRegistry registry;
  92. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  93. for(std::uint64_t i = 0; i < 1000000L; i++) {
  94. const auto entity = registry.create();
  95. registry.assign<Position>(entity);
  96. registry.assign<Velocity>(entity);
  97. }
  98. Timer timer;
  99. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  100. timer.elapsed();
  101. }
  102. TEST(Benchmark, IterateTwoComponents1MHalf) {
  103. entt::DefaultRegistry registry;
  104. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  105. for(std::uint64_t i = 0; i < 1000000L; i++) {
  106. const auto entity = registry.create();
  107. registry.assign<Velocity>(entity);
  108. if(i % 2) {
  109. registry.assign<Position>(entity);
  110. }
  111. }
  112. Timer timer;
  113. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  114. timer.elapsed();
  115. }
  116. TEST(Benchmark, IterateTwoComponents1MOne) {
  117. entt::DefaultRegistry registry;
  118. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  119. for(std::uint64_t i = 0; i < 1000000L; i++) {
  120. const auto entity = registry.create();
  121. registry.assign<Velocity>(entity);
  122. if(i == 5000000L) {
  123. registry.assign<Position>(entity);
  124. }
  125. }
  126. Timer timer;
  127. registry.view<Position, Velocity>().each([](auto, auto &...) {});
  128. timer.elapsed();
  129. }
  130. TEST(Benchmark, IterateTwoComponentsPersistent1M) {
  131. entt::DefaultRegistry registry;
  132. registry.prepare<Position, Velocity>();
  133. std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
  134. for(std::uint64_t i = 0; i < 1000000L; i++) {
  135. const auto entity = registry.create();
  136. registry.assign<Position>(entity);
  137. registry.assign<Velocity>(entity);
  138. }
  139. Timer timer;
  140. registry.view<Position, Velocity>(entt::persistent_t{}).each([](auto, auto &...) {});
  141. timer.elapsed();
  142. }
  143. TEST(Benchmark, IterateFiveComponents1M) {
  144. entt::DefaultRegistry registry;
  145. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  146. for(std::uint64_t i = 0; i < 1000000L; i++) {
  147. const auto entity = registry.create();
  148. registry.assign<Position>(entity);
  149. registry.assign<Velocity>(entity);
  150. registry.assign<Comp<1>>(entity);
  151. registry.assign<Comp<2>>(entity);
  152. registry.assign<Comp<3>>(entity);
  153. }
  154. Timer timer;
  155. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  156. timer.elapsed();
  157. }
  158. TEST(Benchmark, IterateFiveComponents1MHalf) {
  159. entt::DefaultRegistry registry;
  160. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  161. for(std::uint64_t i = 0; i < 1000000L; i++) {
  162. const auto entity = registry.create();
  163. registry.assign<Velocity>(entity);
  164. registry.assign<Comp<1>>(entity);
  165. registry.assign<Comp<2>>(entity);
  166. registry.assign<Comp<3>>(entity);
  167. if(i % 2) {
  168. registry.assign<Position>(entity);
  169. }
  170. }
  171. Timer timer;
  172. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  173. timer.elapsed();
  174. }
  175. TEST(Benchmark, IterateFiveComponents1MOne) {
  176. entt::DefaultRegistry registry;
  177. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  178. for(std::uint64_t i = 0; i < 1000000L; i++) {
  179. const auto entity = registry.create();
  180. registry.assign<Velocity>(entity);
  181. registry.assign<Comp<1>>(entity);
  182. registry.assign<Comp<2>>(entity);
  183. registry.assign<Comp<3>>(entity);
  184. if(i == 5000000L) {
  185. registry.assign<Position>(entity);
  186. }
  187. }
  188. Timer timer;
  189. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
  190. timer.elapsed();
  191. }
  192. TEST(Benchmark, IterateFiveComponentsPersistent1M) {
  193. entt::DefaultRegistry registry;
  194. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  195. std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
  196. for(std::uint64_t i = 0; i < 1000000L; i++) {
  197. const auto entity = registry.create();
  198. registry.assign<Position>(entity);
  199. registry.assign<Velocity>(entity);
  200. registry.assign<Comp<1>>(entity);
  201. registry.assign<Comp<2>>(entity);
  202. registry.assign<Comp<3>>(entity);
  203. }
  204. Timer timer;
  205. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>(entt::persistent_t{}).each([](auto, auto &...) {});
  206. timer.elapsed();
  207. }
  208. TEST(Benchmark, IterateTenComponents1M) {
  209. entt::DefaultRegistry registry;
  210. std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
  211. for(std::uint64_t i = 0; i < 1000000L; i++) {
  212. const auto entity = registry.create();
  213. registry.assign<Position>(entity);
  214. registry.assign<Velocity>(entity);
  215. registry.assign<Comp<1>>(entity);
  216. registry.assign<Comp<2>>(entity);
  217. registry.assign<Comp<3>>(entity);
  218. registry.assign<Comp<4>>(entity);
  219. registry.assign<Comp<5>>(entity);
  220. registry.assign<Comp<6>>(entity);
  221. registry.assign<Comp<7>>(entity);
  222. registry.assign<Comp<8>>(entity);
  223. }
  224. Timer timer;
  225. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  226. timer.elapsed();
  227. }
  228. TEST(Benchmark, IterateTenComponents1MHalf) {
  229. entt::DefaultRegistry registry;
  230. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
  231. for(std::uint64_t i = 0; i < 1000000L; i++) {
  232. const auto entity = registry.create();
  233. registry.assign<Velocity>(entity);
  234. registry.assign<Comp<1>>(entity);
  235. registry.assign<Comp<2>>(entity);
  236. registry.assign<Comp<3>>(entity);
  237. registry.assign<Comp<4>>(entity);
  238. registry.assign<Comp<5>>(entity);
  239. registry.assign<Comp<6>>(entity);
  240. registry.assign<Comp<7>>(entity);
  241. registry.assign<Comp<8>>(entity);
  242. if(i % 2) {
  243. registry.assign<Position>(entity);
  244. }
  245. }
  246. Timer timer;
  247. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  248. timer.elapsed();
  249. }
  250. TEST(Benchmark, IterateTenComponents1MOne) {
  251. entt::DefaultRegistry registry;
  252. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
  253. for(std::uint64_t i = 0; i < 1000000L; i++) {
  254. const auto entity = registry.create();
  255. registry.assign<Velocity>(entity);
  256. registry.assign<Comp<1>>(entity);
  257. registry.assign<Comp<2>>(entity);
  258. registry.assign<Comp<3>>(entity);
  259. registry.assign<Comp<4>>(entity);
  260. registry.assign<Comp<5>>(entity);
  261. registry.assign<Comp<6>>(entity);
  262. registry.assign<Comp<7>>(entity);
  263. registry.assign<Comp<8>>(entity);
  264. if(i == 5000000L) {
  265. registry.assign<Position>(entity);
  266. }
  267. }
  268. Timer timer;
  269. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
  270. timer.elapsed();
  271. }
  272. TEST(Benchmark, IterateTenComponentsPersistent1M) {
  273. entt::DefaultRegistry registry;
  274. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  275. std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
  276. for(std::uint64_t i = 0; i < 1000000L; i++) {
  277. const auto entity = registry.create();
  278. registry.assign<Position>(entity);
  279. registry.assign<Velocity>(entity);
  280. registry.assign<Comp<1>>(entity);
  281. registry.assign<Comp<2>>(entity);
  282. registry.assign<Comp<3>>(entity);
  283. registry.assign<Comp<4>>(entity);
  284. registry.assign<Comp<5>>(entity);
  285. registry.assign<Comp<6>>(entity);
  286. registry.assign<Comp<7>>(entity);
  287. registry.assign<Comp<8>>(entity);
  288. }
  289. Timer timer;
  290. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>(entt::persistent_t{}).each([](auto, auto &...) {});
  291. timer.elapsed();
  292. }
  293. TEST(Benchmark, SortSingle) {
  294. entt::DefaultRegistry registry;
  295. std::cout << "Sort 150000 entities, one component" << std::endl;
  296. for(std::uint64_t i = 0; i < 150000L; i++) {
  297. const auto entity = registry.create();
  298. registry.assign<Position>(entity, i, i);
  299. }
  300. Timer timer;
  301. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  302. return lhs.x < rhs.x && lhs.y < rhs.y;
  303. });
  304. timer.elapsed();
  305. }
  306. TEST(Benchmark, SortMulti) {
  307. entt::DefaultRegistry registry;
  308. std::cout << "Sort 150000 entities, two components" << std::endl;
  309. for(std::uint64_t i = 0; i < 150000L; i++) {
  310. const auto entity = registry.create();
  311. registry.assign<Position>(entity, i, i);
  312. registry.assign<Velocity>(entity, i, i);
  313. }
  314. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  315. return lhs.x < rhs.x && lhs.y < rhs.y;
  316. });
  317. Timer timer;
  318. registry.sort<Velocity, Position>();
  319. timer.elapsed();
  320. }