benchmark.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <chrono>
  5. #include <iterator>
  6. #include <gtest/gtest.h>
  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 { int x; };
  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::registry<> 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, ConstructMany) {
  37. entt::registry<> registry;
  38. std::vector<entt::registry<>::entity_type> entities(1000000);
  39. std::cout << "Constructing 1000000 entities at once" << std::endl;
  40. timer timer;
  41. registry.create(entities.begin(), entities.end());
  42. timer.elapsed();
  43. }
  44. TEST(Benchmark, Destroy) {
  45. entt::registry<> registry;
  46. std::cout << "Destroying 1000000 entities" << std::endl;
  47. for(std::uint64_t i = 0; i < 1000000L; i++) {
  48. registry.create();
  49. }
  50. timer timer;
  51. registry.each([&registry](auto entity) {
  52. registry.destroy(entity);
  53. });
  54. timer.elapsed();
  55. }
  56. TEST(Benchmark, IterateCreateDeleteSingleComponent) {
  57. entt::registry<> registry;
  58. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  59. timer timer;
  60. auto view = registry.view<position>();
  61. for(int i = 0; i < 10000; i++) {
  62. for(int j = 0; j < 10000; j++) {
  63. const auto entity = registry.create();
  64. registry.assign<position>(entity);
  65. }
  66. for(auto entity: view) {
  67. if(rand() % 2 == 0) {
  68. registry.destroy(entity);
  69. }
  70. }
  71. }
  72. timer.elapsed();
  73. }
  74. TEST(Benchmark, IterateSingleComponent1M) {
  75. entt::registry<> registry;
  76. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  77. for(std::uint64_t i = 0; i < 1000000L; i++) {
  78. const auto entity = registry.create();
  79. registry.assign<position>(entity);
  80. }
  81. auto test = [&registry](auto func) {
  82. timer timer;
  83. registry.view<position>().each(func);
  84. timer.elapsed();
  85. };
  86. test([](auto, const auto &) {});
  87. test([](auto, auto &... comp) {
  88. ((comp.x = {}), ...);
  89. });
  90. }
  91. TEST(Benchmark, IterateSingleComponentRaw1M) {
  92. entt::registry<> registry;
  93. std::cout << "Iterating over 1000000 entities, one component, raw view" << std::endl;
  94. for(std::uint64_t i = 0; i < 1000000L; i++) {
  95. const auto entity = registry.create();
  96. registry.assign<position>(entity);
  97. }
  98. auto test = [&registry](auto func) {
  99. timer timer;
  100. registry.raw_view<position>().each(func);
  101. timer.elapsed();
  102. };
  103. test([](const auto &) {});
  104. test([](auto &... comp) {
  105. ((comp.x = {}), ...);
  106. });
  107. }
  108. TEST(Benchmark, IterateSingleComponentRuntime1M) {
  109. entt::registry<> registry;
  110. std::cout << "Iterating over 1000000 entities, one component, runtime view" << std::endl;
  111. for(std::uint64_t i = 0; i < 1000000L; i++) {
  112. const auto entity = registry.create();
  113. registry.assign<position>(entity);
  114. }
  115. auto test = [&registry](auto func) {
  116. using component_type = typename entt::registry<>::component_type;
  117. component_type types[] = { registry.type<position>() };
  118. timer timer;
  119. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  120. timer.elapsed();
  121. };
  122. test([](auto) {});
  123. test([&registry](auto entity) {
  124. registry.get<position>(entity).x = {};
  125. });
  126. }
  127. TEST(Benchmark, IterateTwoComponents1M) {
  128. entt::registry<> registry;
  129. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  130. for(std::uint64_t i = 0; i < 1000000L; i++) {
  131. const auto entity = registry.create();
  132. registry.assign<position>(entity);
  133. registry.assign<velocity>(entity);
  134. }
  135. auto test = [&registry](auto func) {
  136. timer timer;
  137. registry.view<position, velocity>().each(func);
  138. timer.elapsed();
  139. };
  140. test([](auto, const auto &...) {});
  141. test([](auto, auto &... comp) {
  142. ((comp.x = {}), ...);
  143. });
  144. }
  145. TEST(Benchmark, IterateTwoComponents1MHalf) {
  146. entt::registry<> registry;
  147. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  148. for(std::uint64_t i = 0; i < 1000000L; i++) {
  149. const auto entity = registry.create();
  150. registry.assign<velocity>(entity);
  151. if(i % 2) {
  152. registry.assign<position>(entity);
  153. }
  154. }
  155. auto test = [&registry](auto func) {
  156. timer timer;
  157. registry.view<position, velocity>().each(func);
  158. timer.elapsed();
  159. };
  160. test([](auto, const auto &...) {});
  161. test([](auto, auto &... comp) {
  162. ((comp.x = {}), ...);
  163. });
  164. }
  165. TEST(Benchmark, IterateTwoComponents1MOne) {
  166. entt::registry<> registry;
  167. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  168. for(std::uint64_t i = 0; i < 1000000L; i++) {
  169. const auto entity = registry.create();
  170. registry.assign<velocity>(entity);
  171. if(i == 5000000L) {
  172. registry.assign<position>(entity);
  173. }
  174. }
  175. auto test = [&registry](auto func) {
  176. timer timer;
  177. registry.view<position, velocity>().each(func);
  178. timer.elapsed();
  179. };
  180. test([](auto, const auto &...) {});
  181. test([](auto, auto &... comp) {
  182. ((comp.x = {}), ...);
  183. });
  184. }
  185. TEST(Benchmark, IterateTwoComponentsPersistent1M) {
  186. entt::registry<> registry;
  187. registry.prepare_persistent_view<position, velocity>();
  188. std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
  189. for(std::uint64_t i = 0; i < 1000000L; i++) {
  190. const auto entity = registry.create();
  191. registry.assign<position>(entity);
  192. registry.assign<velocity>(entity);
  193. }
  194. auto test = [&registry](auto func) {
  195. timer timer;
  196. registry.persistent_view<position, velocity>().each(func);
  197. timer.elapsed();
  198. };
  199. test([](auto, const auto &...) {});
  200. test([](auto, auto &... comp) {
  201. ((comp.x = {}), ...);
  202. });
  203. }
  204. TEST(Benchmark, IterateTwoComponentsRuntime1M) {
  205. entt::registry<> registry;
  206. registry.prepare_persistent_view<position, velocity>();
  207. std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
  208. for(std::uint64_t i = 0; i < 1000000L; i++) {
  209. const auto entity = registry.create();
  210. registry.assign<position>(entity);
  211. registry.assign<velocity>(entity);
  212. }
  213. auto test = [&registry](auto func) {
  214. using component_type = typename entt::registry<>::component_type;
  215. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  216. timer timer;
  217. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  218. timer.elapsed();
  219. };
  220. test([](auto) {});
  221. test([&registry](auto entity) {
  222. registry.get<position>(entity).x = {};
  223. registry.get<velocity>(entity).x = {};
  224. });
  225. }
  226. TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
  227. entt::registry<> registry;
  228. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
  229. for(std::uint64_t i = 0; i < 1000000L; i++) {
  230. const auto entity = registry.create();
  231. registry.assign<velocity>(entity);
  232. if(i % 2) {
  233. registry.assign<position>(entity);
  234. }
  235. }
  236. auto test = [&registry](auto func) {
  237. using component_type = typename entt::registry<>::component_type;
  238. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  239. timer timer;
  240. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  241. timer.elapsed();
  242. };
  243. test([](auto) {});
  244. test([&registry](auto entity) {
  245. registry.get<position>(entity).x = {};
  246. registry.get<velocity>(entity).x = {};
  247. });
  248. }
  249. TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
  250. entt::registry<> registry;
  251. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
  252. for(std::uint64_t i = 0; i < 1000000L; i++) {
  253. const auto entity = registry.create();
  254. registry.assign<velocity>(entity);
  255. if(i == 5000000L) {
  256. registry.assign<position>(entity);
  257. }
  258. }
  259. auto test = [&registry](auto func) {
  260. using component_type = typename entt::registry<>::component_type;
  261. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  262. timer timer;
  263. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  264. timer.elapsed();
  265. };
  266. test([](auto) {});
  267. test([&registry](auto entity) {
  268. registry.get<position>(entity).x = {};
  269. registry.get<velocity>(entity).x = {};
  270. });
  271. }
  272. TEST(Benchmark, IterateFiveComponents1M) {
  273. entt::registry<> registry;
  274. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  275. for(std::uint64_t i = 0; i < 1000000L; i++) {
  276. const auto entity = registry.create();
  277. registry.assign<position>(entity);
  278. registry.assign<velocity>(entity);
  279. registry.assign<comp<1>>(entity);
  280. registry.assign<comp<2>>(entity);
  281. registry.assign<comp<3>>(entity);
  282. }
  283. auto test = [&registry](auto func) {
  284. timer timer;
  285. registry.view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  286. timer.elapsed();
  287. };
  288. test([](auto, const auto &...) {});
  289. test([](auto, auto &... comp) {
  290. ((comp.x = {}), ...);
  291. });
  292. }
  293. TEST(Benchmark, IterateFiveComponents1MHalf) {
  294. entt::registry<> registry;
  295. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  296. for(std::uint64_t i = 0; i < 1000000L; i++) {
  297. const auto entity = registry.create();
  298. registry.assign<velocity>(entity);
  299. registry.assign<comp<1>>(entity);
  300. registry.assign<comp<2>>(entity);
  301. registry.assign<comp<3>>(entity);
  302. if(i % 2) {
  303. registry.assign<position>(entity);
  304. }
  305. }
  306. auto test = [&registry](auto func) {
  307. timer timer;
  308. registry.view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  309. timer.elapsed();
  310. };
  311. test([](auto, const auto &...) {});
  312. test([](auto, auto &... comp) {
  313. ((comp.x = {}), ...);
  314. });
  315. }
  316. TEST(Benchmark, IterateFiveComponents1MOne) {
  317. entt::registry<> registry;
  318. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  319. for(std::uint64_t i = 0; i < 1000000L; i++) {
  320. const auto entity = registry.create();
  321. registry.assign<velocity>(entity);
  322. registry.assign<comp<1>>(entity);
  323. registry.assign<comp<2>>(entity);
  324. registry.assign<comp<3>>(entity);
  325. if(i == 5000000L) {
  326. registry.assign<position>(entity);
  327. }
  328. }
  329. auto test = [&registry](auto func) {
  330. timer timer;
  331. registry.view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  332. timer.elapsed();
  333. };
  334. test([](auto, const auto &...) {});
  335. test([](auto, auto &... comp) {
  336. ((comp.x = {}), ...);
  337. });
  338. }
  339. TEST(Benchmark, IterateFiveComponentsPersistent1M) {
  340. entt::registry<> registry;
  341. registry.prepare_persistent_view<position, velocity, comp<1>, comp<2>, comp<3>>();
  342. std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
  343. for(std::uint64_t i = 0; i < 1000000L; i++) {
  344. const auto entity = registry.create();
  345. registry.assign<position>(entity);
  346. registry.assign<velocity>(entity);
  347. registry.assign<comp<1>>(entity);
  348. registry.assign<comp<2>>(entity);
  349. registry.assign<comp<3>>(entity);
  350. }
  351. auto test = [&registry](auto func) {
  352. timer timer;
  353. registry.persistent_view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  354. timer.elapsed();
  355. };
  356. test([](auto, const auto &...) {});
  357. test([](auto, auto &... comp) {
  358. ((comp.x = {}), ...);
  359. });
  360. }
  361. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  362. entt::registry<> registry;
  363. registry.prepare_persistent_view<position, velocity, comp<1>, comp<2>, comp<3>>();
  364. std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
  365. for(std::uint64_t i = 0; i < 1000000L; i++) {
  366. const auto entity = registry.create();
  367. registry.assign<position>(entity);
  368. registry.assign<velocity>(entity);
  369. registry.assign<comp<1>>(entity);
  370. registry.assign<comp<2>>(entity);
  371. registry.assign<comp<3>>(entity);
  372. }
  373. auto test = [&registry](auto func) {
  374. using component_type = typename entt::registry<>::component_type;
  375. component_type types[] = {
  376. registry.type<position>(),
  377. registry.type<velocity>(),
  378. registry.type<comp<1>>(),
  379. registry.type<comp<2>>(),
  380. registry.type<comp<3>>()
  381. };
  382. timer timer;
  383. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  384. timer.elapsed();
  385. };
  386. test([](auto) {});
  387. test([&registry](auto entity) {
  388. registry.get<position>(entity).x = {};
  389. registry.get<velocity>(entity).x = {};
  390. registry.get<comp<1>>(entity).x = {};
  391. registry.get<comp<2>>(entity).x = {};
  392. registry.get<comp<3>>(entity).x = {};
  393. });
  394. }
  395. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  396. entt::registry<> registry;
  397. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  398. for(std::uint64_t i = 0; i < 1000000L; i++) {
  399. const auto entity = registry.create();
  400. registry.assign<velocity>(entity);
  401. registry.assign<comp<1>>(entity);
  402. registry.assign<comp<2>>(entity);
  403. registry.assign<comp<3>>(entity);
  404. if(i % 2) {
  405. registry.assign<position>(entity);
  406. }
  407. }
  408. auto test = [&registry](auto func) {
  409. using component_type = typename entt::registry<>::component_type;
  410. component_type types[] = {
  411. registry.type<position>(),
  412. registry.type<velocity>(),
  413. registry.type<comp<1>>(),
  414. registry.type<comp<2>>(),
  415. registry.type<comp<3>>()
  416. };
  417. timer timer;
  418. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  419. timer.elapsed();
  420. };
  421. test([](auto) {});
  422. test([&registry](auto entity) {
  423. registry.get<position>(entity).x = {};
  424. registry.get<velocity>(entity).x = {};
  425. registry.get<comp<1>>(entity).x = {};
  426. registry.get<comp<2>>(entity).x = {};
  427. registry.get<comp<3>>(entity).x = {};
  428. });
  429. }
  430. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  431. entt::registry<> registry;
  432. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  433. for(std::uint64_t i = 0; i < 1000000L; i++) {
  434. const auto entity = registry.create();
  435. registry.assign<velocity>(entity);
  436. registry.assign<comp<1>>(entity);
  437. registry.assign<comp<2>>(entity);
  438. registry.assign<comp<3>>(entity);
  439. if(i == 5000000L) {
  440. registry.assign<position>(entity);
  441. }
  442. }
  443. auto test = [&registry](auto func) {
  444. using component_type = typename entt::registry<>::component_type;
  445. component_type types[] = {
  446. registry.type<position>(),
  447. registry.type<velocity>(),
  448. registry.type<comp<1>>(),
  449. registry.type<comp<2>>(),
  450. registry.type<comp<3>>()
  451. };
  452. timer timer;
  453. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  454. timer.elapsed();
  455. };
  456. test([](auto) {});
  457. test([&registry](auto entity) {
  458. registry.get<position>(entity).x = {};
  459. registry.get<velocity>(entity).x = {};
  460. registry.get<comp<1>>(entity).x = {};
  461. registry.get<comp<2>>(entity).x = {};
  462. registry.get<comp<3>>(entity).x = {};
  463. });
  464. }
  465. TEST(Benchmark, IterateTenComponents1M) {
  466. entt::registry<> registry;
  467. std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
  468. for(std::uint64_t i = 0; i < 1000000L; i++) {
  469. const auto entity = registry.create();
  470. registry.assign<position>(entity);
  471. registry.assign<velocity>(entity);
  472. registry.assign<comp<1>>(entity);
  473. registry.assign<comp<2>>(entity);
  474. registry.assign<comp<3>>(entity);
  475. registry.assign<comp<4>>(entity);
  476. registry.assign<comp<5>>(entity);
  477. registry.assign<comp<6>>(entity);
  478. registry.assign<comp<7>>(entity);
  479. registry.assign<comp<8>>(entity);
  480. }
  481. auto test = [&registry](auto func) {
  482. timer timer;
  483. registry.view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  484. timer.elapsed();
  485. };
  486. test([](auto, const auto &...) {});
  487. test([](auto, auto &... comp) {
  488. ((comp.x = {}), ...);
  489. });
  490. }
  491. TEST(Benchmark, IterateTenComponents1MHalf) {
  492. entt::registry<> registry;
  493. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
  494. for(std::uint64_t i = 0; i < 1000000L; i++) {
  495. const auto entity = registry.create();
  496. registry.assign<velocity>(entity);
  497. registry.assign<comp<1>>(entity);
  498. registry.assign<comp<2>>(entity);
  499. registry.assign<comp<3>>(entity);
  500. registry.assign<comp<4>>(entity);
  501. registry.assign<comp<5>>(entity);
  502. registry.assign<comp<6>>(entity);
  503. registry.assign<comp<7>>(entity);
  504. registry.assign<comp<8>>(entity);
  505. if(i % 2) {
  506. registry.assign<position>(entity);
  507. }
  508. }
  509. auto test = [&registry](auto func) {
  510. timer timer;
  511. registry.view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  512. timer.elapsed();
  513. };
  514. test([](auto, auto &...) {});
  515. test([](auto, auto &... comp) {
  516. ((comp.x = {}), ...);
  517. });
  518. }
  519. TEST(Benchmark, IterateTenComponents1MOne) {
  520. entt::registry<> registry;
  521. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
  522. for(std::uint64_t i = 0; i < 1000000L; i++) {
  523. const auto entity = registry.create();
  524. registry.assign<velocity>(entity);
  525. registry.assign<comp<1>>(entity);
  526. registry.assign<comp<2>>(entity);
  527. registry.assign<comp<3>>(entity);
  528. registry.assign<comp<4>>(entity);
  529. registry.assign<comp<5>>(entity);
  530. registry.assign<comp<6>>(entity);
  531. registry.assign<comp<7>>(entity);
  532. registry.assign<comp<8>>(entity);
  533. if(i == 5000000L) {
  534. registry.assign<position>(entity);
  535. }
  536. }
  537. auto test = [&registry](auto func) {
  538. timer timer;
  539. registry.view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  540. timer.elapsed();
  541. };
  542. test([](auto, const auto &...) {});
  543. test([](auto, auto &... comp) {
  544. ((comp.x = {}), ...);
  545. });
  546. }
  547. TEST(Benchmark, IterateTenComponentsPersistent1M) {
  548. entt::registry<> registry;
  549. registry.prepare_persistent_view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>();
  550. std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
  551. for(std::uint64_t i = 0; i < 1000000L; i++) {
  552. const auto entity = registry.create();
  553. registry.assign<position>(entity);
  554. registry.assign<velocity>(entity);
  555. registry.assign<comp<1>>(entity);
  556. registry.assign<comp<2>>(entity);
  557. registry.assign<comp<3>>(entity);
  558. registry.assign<comp<4>>(entity);
  559. registry.assign<comp<5>>(entity);
  560. registry.assign<comp<6>>(entity);
  561. registry.assign<comp<7>>(entity);
  562. registry.assign<comp<8>>(entity);
  563. }
  564. auto test = [&registry](auto func) {
  565. timer timer;
  566. registry.persistent_view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  567. timer.elapsed();
  568. };
  569. test([](auto, const auto &...) {});
  570. test([](auto, auto &... comp) {
  571. ((comp.x = {}), ...);
  572. });
  573. }
  574. TEST(Benchmark, IterateTenComponentsRuntime1M) {
  575. entt::registry<> registry;
  576. registry.prepare_persistent_view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>();
  577. std::cout << "Iterating over 1000000 entities, ten components, runtime view" << std::endl;
  578. for(std::uint64_t i = 0; i < 1000000L; i++) {
  579. const auto entity = registry.create();
  580. registry.assign<position>(entity);
  581. registry.assign<velocity>(entity);
  582. registry.assign<comp<1>>(entity);
  583. registry.assign<comp<2>>(entity);
  584. registry.assign<comp<3>>(entity);
  585. registry.assign<comp<4>>(entity);
  586. registry.assign<comp<5>>(entity);
  587. registry.assign<comp<6>>(entity);
  588. registry.assign<comp<7>>(entity);
  589. registry.assign<comp<8>>(entity);
  590. }
  591. auto test = [&registry](auto func) {
  592. using component_type = typename entt::registry<>::component_type;
  593. component_type types[] = {
  594. registry.type<position>(),
  595. registry.type<velocity>(),
  596. registry.type<comp<1>>(),
  597. registry.type<comp<2>>(),
  598. registry.type<comp<3>>(),
  599. registry.type<comp<4>>(),
  600. registry.type<comp<5>>(),
  601. registry.type<comp<6>>(),
  602. registry.type<comp<7>>(),
  603. registry.type<comp<8>>()
  604. };
  605. timer timer;
  606. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  607. timer.elapsed();
  608. };
  609. test([](auto) {});
  610. test([&registry](auto entity) {
  611. registry.get<position>(entity).x = {};
  612. registry.get<velocity>(entity).x = {};
  613. registry.get<comp<1>>(entity).x = {};
  614. registry.get<comp<2>>(entity).x = {};
  615. registry.get<comp<3>>(entity).x = {};
  616. registry.get<comp<4>>(entity).x = {};
  617. registry.get<comp<5>>(entity).x = {};
  618. registry.get<comp<6>>(entity).x = {};
  619. registry.get<comp<7>>(entity).x = {};
  620. registry.get<comp<8>>(entity).x = {};
  621. });
  622. }
  623. TEST(Benchmark, IterateTenComponentsRuntime1MHalf) {
  624. entt::registry<> registry;
  625. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components, runtime view" << std::endl;
  626. for(std::uint64_t i = 0; i < 1000000L; i++) {
  627. const auto entity = registry.create();
  628. registry.assign<velocity>(entity);
  629. registry.assign<comp<1>>(entity);
  630. registry.assign<comp<2>>(entity);
  631. registry.assign<comp<3>>(entity);
  632. registry.assign<comp<4>>(entity);
  633. registry.assign<comp<5>>(entity);
  634. registry.assign<comp<6>>(entity);
  635. registry.assign<comp<7>>(entity);
  636. registry.assign<comp<8>>(entity);
  637. if(i % 2) {
  638. registry.assign<position>(entity);
  639. }
  640. }
  641. auto test = [&registry](auto func) {
  642. using component_type = typename entt::registry<>::component_type;
  643. component_type types[] = {
  644. registry.type<position>(),
  645. registry.type<velocity>(),
  646. registry.type<comp<1>>(),
  647. registry.type<comp<2>>(),
  648. registry.type<comp<3>>(),
  649. registry.type<comp<4>>(),
  650. registry.type<comp<5>>(),
  651. registry.type<comp<6>>(),
  652. registry.type<comp<7>>(),
  653. registry.type<comp<8>>()
  654. };
  655. timer timer;
  656. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  657. timer.elapsed();
  658. };
  659. test([](auto) {});
  660. test([&registry](auto entity) {
  661. registry.get<position>(entity).x = {};
  662. registry.get<velocity>(entity).x = {};
  663. registry.get<comp<1>>(entity).x = {};
  664. registry.get<comp<2>>(entity).x = {};
  665. registry.get<comp<3>>(entity).x = {};
  666. registry.get<comp<4>>(entity).x = {};
  667. registry.get<comp<5>>(entity).x = {};
  668. registry.get<comp<6>>(entity).x = {};
  669. registry.get<comp<7>>(entity).x = {};
  670. registry.get<comp<8>>(entity).x = {};
  671. });
  672. }
  673. TEST(Benchmark, IterateTenComponentsRuntime1MOne) {
  674. entt::registry<> registry;
  675. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components, runtime view" << std::endl;
  676. for(std::uint64_t i = 0; i < 1000000L; i++) {
  677. const auto entity = registry.create();
  678. registry.assign<velocity>(entity);
  679. registry.assign<comp<1>>(entity);
  680. registry.assign<comp<2>>(entity);
  681. registry.assign<comp<3>>(entity);
  682. registry.assign<comp<4>>(entity);
  683. registry.assign<comp<5>>(entity);
  684. registry.assign<comp<6>>(entity);
  685. registry.assign<comp<7>>(entity);
  686. registry.assign<comp<8>>(entity);
  687. if(i == 5000000L) {
  688. registry.assign<position>(entity);
  689. }
  690. }
  691. auto test = [&registry](auto func) {
  692. using component_type = typename entt::registry<>::component_type;
  693. component_type types[] = {
  694. registry.type<position>(),
  695. registry.type<velocity>(),
  696. registry.type<comp<1>>(),
  697. registry.type<comp<2>>(),
  698. registry.type<comp<3>>(),
  699. registry.type<comp<4>>(),
  700. registry.type<comp<5>>(),
  701. registry.type<comp<6>>(),
  702. registry.type<comp<7>>(),
  703. registry.type<comp<8>>()
  704. };
  705. timer timer;
  706. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  707. timer.elapsed();
  708. };
  709. test([](auto) {});
  710. test([&registry](auto entity) {
  711. registry.get<position>(entity).x = {};
  712. registry.get<velocity>(entity).x = {};
  713. registry.get<comp<1>>(entity).x = {};
  714. registry.get<comp<2>>(entity).x = {};
  715. registry.get<comp<3>>(entity).x = {};
  716. registry.get<comp<4>>(entity).x = {};
  717. registry.get<comp<5>>(entity).x = {};
  718. registry.get<comp<6>>(entity).x = {};
  719. registry.get<comp<7>>(entity).x = {};
  720. registry.get<comp<8>>(entity).x = {};
  721. });
  722. }
  723. TEST(Benchmark, SortSingle) {
  724. entt::registry<> registry;
  725. std::cout << "Sort 150000 entities, one component" << std::endl;
  726. for(std::uint64_t i = 0; i < 150000L; i++) {
  727. const auto entity = registry.create();
  728. registry.assign<position>(entity, i, i);
  729. }
  730. timer timer;
  731. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  732. return lhs.x < rhs.x && lhs.y < rhs.y;
  733. });
  734. timer.elapsed();
  735. }
  736. TEST(Benchmark, SortMulti) {
  737. entt::registry<> registry;
  738. std::cout << "Sort 150000 entities, two components" << std::endl;
  739. for(std::uint64_t i = 0; i < 150000L; i++) {
  740. const auto entity = registry.create();
  741. registry.assign<position>(entity, i, i);
  742. registry.assign<velocity>(entity, i, i);
  743. }
  744. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  745. return lhs.x < rhs.x && lhs.y < rhs.y;
  746. });
  747. timer timer;
  748. registry.sort<velocity, position>();
  749. timer.elapsed();
  750. }
  751. TEST(Benchmark, AlmostSortedStdSort) {
  752. entt::registry<> registry;
  753. entt::registry<>::entity_type entities[3];
  754. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  755. for(std::uint64_t i = 0; i < 150000L; i++) {
  756. const auto entity = registry.create();
  757. registry.assign<position>(entity, i, i);
  758. if(!(i % 50000)) {
  759. entities[i / 50000] = entity;
  760. }
  761. }
  762. for(std::uint64_t i = 0; i < 3; ++i) {
  763. registry.destroy(entities[i]);
  764. const auto entity = registry.create();
  765. registry.assign<position>(entity, 50000 * i, 50000 * i);
  766. }
  767. timer timer;
  768. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  769. return lhs.x > rhs.x && lhs.y > rhs.y;
  770. });
  771. timer.elapsed();
  772. }
  773. TEST(Benchmark, AlmostSortedInsertionSort) {
  774. entt::registry<> registry;
  775. entt::registry<>::entity_type entities[3];
  776. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
  777. for(std::uint64_t i = 0; i < 150000L; i++) {
  778. const auto entity = registry.create();
  779. registry.assign<position>(entity, i, i);
  780. if(!(i % 50000)) {
  781. entities[i / 50000] = entity;
  782. }
  783. }
  784. for(std::uint64_t i = 0; i < 3; ++i) {
  785. registry.destroy(entities[i]);
  786. const auto entity = registry.create();
  787. registry.assign<position>(entity, 50000 * i, 50000 * i);
  788. }
  789. timer timer;
  790. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  791. return lhs.x > rhs.x && lhs.y > rhs.y;
  792. }, entt::insertion_sort{});
  793. timer.elapsed();
  794. }