benchmark.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
  188. for(std::uint64_t i = 0; i < 1000000L; i++) {
  189. const auto entity = registry.create();
  190. registry.assign<position>(entity);
  191. registry.assign<velocity>(entity);
  192. }
  193. auto test = [&registry](auto func) {
  194. timer timer;
  195. registry.persistent_view<position, velocity>().each(func);
  196. timer.elapsed();
  197. };
  198. test([](auto, const auto &...) {});
  199. test([](auto, auto &... comp) {
  200. ((comp.x = {}), ...);
  201. });
  202. }
  203. TEST(Benchmark, IterateTwoComponentsRuntime1M) {
  204. entt::registry<> registry;
  205. std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
  206. for(std::uint64_t i = 0; i < 1000000L; i++) {
  207. const auto entity = registry.create();
  208. registry.assign<position>(entity);
  209. registry.assign<velocity>(entity);
  210. }
  211. auto test = [&registry](auto func) {
  212. using component_type = typename entt::registry<>::component_type;
  213. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  214. timer timer;
  215. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  216. timer.elapsed();
  217. };
  218. test([](auto) {});
  219. test([&registry](auto entity) {
  220. registry.get<position>(entity).x = {};
  221. registry.get<velocity>(entity).x = {};
  222. });
  223. }
  224. TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
  225. entt::registry<> registry;
  226. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
  227. for(std::uint64_t i = 0; i < 1000000L; i++) {
  228. const auto entity = registry.create();
  229. registry.assign<velocity>(entity);
  230. if(i % 2) {
  231. registry.assign<position>(entity);
  232. }
  233. }
  234. auto test = [&registry](auto func) {
  235. using component_type = typename entt::registry<>::component_type;
  236. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  237. timer timer;
  238. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  239. timer.elapsed();
  240. };
  241. test([](auto) {});
  242. test([&registry](auto entity) {
  243. registry.get<position>(entity).x = {};
  244. registry.get<velocity>(entity).x = {};
  245. });
  246. }
  247. TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
  248. entt::registry<> registry;
  249. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
  250. for(std::uint64_t i = 0; i < 1000000L; i++) {
  251. const auto entity = registry.create();
  252. registry.assign<velocity>(entity);
  253. if(i == 5000000L) {
  254. registry.assign<position>(entity);
  255. }
  256. }
  257. auto test = [&registry](auto func) {
  258. using component_type = typename entt::registry<>::component_type;
  259. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  260. timer timer;
  261. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  262. timer.elapsed();
  263. };
  264. test([](auto) {});
  265. test([&registry](auto entity) {
  266. registry.get<position>(entity).x = {};
  267. registry.get<velocity>(entity).x = {};
  268. });
  269. }
  270. TEST(Benchmark, IterateFiveComponents1M) {
  271. entt::registry<> registry;
  272. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  273. for(std::uint64_t i = 0; i < 1000000L; i++) {
  274. const auto entity = registry.create();
  275. registry.assign<position>(entity);
  276. registry.assign<velocity>(entity);
  277. registry.assign<comp<1>>(entity);
  278. registry.assign<comp<2>>(entity);
  279. registry.assign<comp<3>>(entity);
  280. }
  281. auto test = [&registry](auto func) {
  282. timer timer;
  283. registry.view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  284. timer.elapsed();
  285. };
  286. test([](auto, const auto &...) {});
  287. test([](auto, auto &... comp) {
  288. ((comp.x = {}), ...);
  289. });
  290. }
  291. TEST(Benchmark, IterateFiveComponents1MHalf) {
  292. entt::registry<> registry;
  293. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  294. for(std::uint64_t i = 0; i < 1000000L; i++) {
  295. const auto entity = registry.create();
  296. registry.assign<velocity>(entity);
  297. registry.assign<comp<1>>(entity);
  298. registry.assign<comp<2>>(entity);
  299. registry.assign<comp<3>>(entity);
  300. if(i % 2) {
  301. registry.assign<position>(entity);
  302. }
  303. }
  304. auto test = [&registry](auto func) {
  305. timer timer;
  306. registry.view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  307. timer.elapsed();
  308. };
  309. test([](auto, const auto &...) {});
  310. test([](auto, auto &... comp) {
  311. ((comp.x = {}), ...);
  312. });
  313. }
  314. TEST(Benchmark, IterateFiveComponents1MOne) {
  315. entt::registry<> registry;
  316. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  317. for(std::uint64_t i = 0; i < 1000000L; i++) {
  318. const auto entity = registry.create();
  319. registry.assign<velocity>(entity);
  320. registry.assign<comp<1>>(entity);
  321. registry.assign<comp<2>>(entity);
  322. registry.assign<comp<3>>(entity);
  323. if(i == 5000000L) {
  324. registry.assign<position>(entity);
  325. }
  326. }
  327. auto test = [&registry](auto func) {
  328. timer timer;
  329. registry.view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  330. timer.elapsed();
  331. };
  332. test([](auto, const auto &...) {});
  333. test([](auto, auto &... comp) {
  334. ((comp.x = {}), ...);
  335. });
  336. }
  337. TEST(Benchmark, IterateFiveComponentsPersistent1M) {
  338. entt::registry<> registry;
  339. std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
  340. for(std::uint64_t i = 0; i < 1000000L; i++) {
  341. const auto entity = registry.create();
  342. registry.assign<position>(entity);
  343. registry.assign<velocity>(entity);
  344. registry.assign<comp<1>>(entity);
  345. registry.assign<comp<2>>(entity);
  346. registry.assign<comp<3>>(entity);
  347. }
  348. auto test = [&registry](auto func) {
  349. timer timer;
  350. registry.persistent_view<position, velocity, comp<1>, comp<2>, comp<3>>().each(func);
  351. timer.elapsed();
  352. };
  353. test([](auto, const auto &...) {});
  354. test([](auto, auto &... comp) {
  355. ((comp.x = {}), ...);
  356. });
  357. }
  358. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  359. entt::registry<> registry;
  360. std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
  361. for(std::uint64_t i = 0; i < 1000000L; i++) {
  362. const auto entity = registry.create();
  363. registry.assign<position>(entity);
  364. registry.assign<velocity>(entity);
  365. registry.assign<comp<1>>(entity);
  366. registry.assign<comp<2>>(entity);
  367. registry.assign<comp<3>>(entity);
  368. }
  369. auto test = [&registry](auto func) {
  370. using component_type = typename entt::registry<>::component_type;
  371. component_type types[] = {
  372. registry.type<position>(),
  373. registry.type<velocity>(),
  374. registry.type<comp<1>>(),
  375. registry.type<comp<2>>(),
  376. registry.type<comp<3>>()
  377. };
  378. timer timer;
  379. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  380. timer.elapsed();
  381. };
  382. test([](auto) {});
  383. test([&registry](auto entity) {
  384. registry.get<position>(entity).x = {};
  385. registry.get<velocity>(entity).x = {};
  386. registry.get<comp<1>>(entity).x = {};
  387. registry.get<comp<2>>(entity).x = {};
  388. registry.get<comp<3>>(entity).x = {};
  389. });
  390. }
  391. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  392. entt::registry<> registry;
  393. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  394. for(std::uint64_t i = 0; i < 1000000L; i++) {
  395. const auto entity = registry.create();
  396. registry.assign<velocity>(entity);
  397. registry.assign<comp<1>>(entity);
  398. registry.assign<comp<2>>(entity);
  399. registry.assign<comp<3>>(entity);
  400. if(i % 2) {
  401. registry.assign<position>(entity);
  402. }
  403. }
  404. auto test = [&registry](auto func) {
  405. using component_type = typename entt::registry<>::component_type;
  406. component_type types[] = {
  407. registry.type<position>(),
  408. registry.type<velocity>(),
  409. registry.type<comp<1>>(),
  410. registry.type<comp<2>>(),
  411. registry.type<comp<3>>()
  412. };
  413. timer timer;
  414. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  415. timer.elapsed();
  416. };
  417. test([](auto) {});
  418. test([&registry](auto entity) {
  419. registry.get<position>(entity).x = {};
  420. registry.get<velocity>(entity).x = {};
  421. registry.get<comp<1>>(entity).x = {};
  422. registry.get<comp<2>>(entity).x = {};
  423. registry.get<comp<3>>(entity).x = {};
  424. });
  425. }
  426. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  427. entt::registry<> registry;
  428. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  429. for(std::uint64_t i = 0; i < 1000000L; i++) {
  430. const auto entity = registry.create();
  431. registry.assign<velocity>(entity);
  432. registry.assign<comp<1>>(entity);
  433. registry.assign<comp<2>>(entity);
  434. registry.assign<comp<3>>(entity);
  435. if(i == 5000000L) {
  436. registry.assign<position>(entity);
  437. }
  438. }
  439. auto test = [&registry](auto func) {
  440. using component_type = typename entt::registry<>::component_type;
  441. component_type types[] = {
  442. registry.type<position>(),
  443. registry.type<velocity>(),
  444. registry.type<comp<1>>(),
  445. registry.type<comp<2>>(),
  446. registry.type<comp<3>>()
  447. };
  448. timer timer;
  449. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  450. timer.elapsed();
  451. };
  452. test([](auto) {});
  453. test([&registry](auto entity) {
  454. registry.get<position>(entity).x = {};
  455. registry.get<velocity>(entity).x = {};
  456. registry.get<comp<1>>(entity).x = {};
  457. registry.get<comp<2>>(entity).x = {};
  458. registry.get<comp<3>>(entity).x = {};
  459. });
  460. }
  461. TEST(Benchmark, IterateTenComponents1M) {
  462. entt::registry<> registry;
  463. std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
  464. for(std::uint64_t i = 0; i < 1000000L; i++) {
  465. const auto entity = registry.create();
  466. registry.assign<position>(entity);
  467. registry.assign<velocity>(entity);
  468. registry.assign<comp<1>>(entity);
  469. registry.assign<comp<2>>(entity);
  470. registry.assign<comp<3>>(entity);
  471. registry.assign<comp<4>>(entity);
  472. registry.assign<comp<5>>(entity);
  473. registry.assign<comp<6>>(entity);
  474. registry.assign<comp<7>>(entity);
  475. registry.assign<comp<8>>(entity);
  476. }
  477. auto test = [&registry](auto func) {
  478. timer timer;
  479. registry.view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  480. timer.elapsed();
  481. };
  482. test([](auto, const auto &...) {});
  483. test([](auto, auto &... comp) {
  484. ((comp.x = {}), ...);
  485. });
  486. }
  487. TEST(Benchmark, IterateTenComponents1MHalf) {
  488. entt::registry<> registry;
  489. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
  490. for(std::uint64_t i = 0; i < 1000000L; i++) {
  491. const auto entity = registry.create();
  492. registry.assign<velocity>(entity);
  493. registry.assign<comp<1>>(entity);
  494. registry.assign<comp<2>>(entity);
  495. registry.assign<comp<3>>(entity);
  496. registry.assign<comp<4>>(entity);
  497. registry.assign<comp<5>>(entity);
  498. registry.assign<comp<6>>(entity);
  499. registry.assign<comp<7>>(entity);
  500. registry.assign<comp<8>>(entity);
  501. if(i % 2) {
  502. registry.assign<position>(entity);
  503. }
  504. }
  505. auto test = [&registry](auto func) {
  506. timer timer;
  507. registry.view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  508. timer.elapsed();
  509. };
  510. test([](auto, auto &...) {});
  511. test([](auto, auto &... comp) {
  512. ((comp.x = {}), ...);
  513. });
  514. }
  515. TEST(Benchmark, IterateTenComponents1MOne) {
  516. entt::registry<> registry;
  517. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
  518. for(std::uint64_t i = 0; i < 1000000L; i++) {
  519. const auto entity = registry.create();
  520. registry.assign<velocity>(entity);
  521. registry.assign<comp<1>>(entity);
  522. registry.assign<comp<2>>(entity);
  523. registry.assign<comp<3>>(entity);
  524. registry.assign<comp<4>>(entity);
  525. registry.assign<comp<5>>(entity);
  526. registry.assign<comp<6>>(entity);
  527. registry.assign<comp<7>>(entity);
  528. registry.assign<comp<8>>(entity);
  529. if(i == 5000000L) {
  530. registry.assign<position>(entity);
  531. }
  532. }
  533. auto test = [&registry](auto func) {
  534. timer timer;
  535. registry.view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  536. timer.elapsed();
  537. };
  538. test([](auto, const auto &...) {});
  539. test([](auto, auto &... comp) {
  540. ((comp.x = {}), ...);
  541. });
  542. }
  543. TEST(Benchmark, IterateTenComponentsPersistent1M) {
  544. entt::registry<> registry;
  545. std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
  546. for(std::uint64_t i = 0; i < 1000000L; i++) {
  547. const auto entity = registry.create();
  548. registry.assign<position>(entity);
  549. registry.assign<velocity>(entity);
  550. registry.assign<comp<1>>(entity);
  551. registry.assign<comp<2>>(entity);
  552. registry.assign<comp<3>>(entity);
  553. registry.assign<comp<4>>(entity);
  554. registry.assign<comp<5>>(entity);
  555. registry.assign<comp<6>>(entity);
  556. registry.assign<comp<7>>(entity);
  557. registry.assign<comp<8>>(entity);
  558. }
  559. auto test = [&registry](auto func) {
  560. timer timer;
  561. registry.persistent_view<position, velocity, comp<1>, comp<2>, comp<3>, comp<4>, comp<5>, comp<6>, comp<7>, comp<8>>().each(func);
  562. timer.elapsed();
  563. };
  564. test([](auto, const auto &...) {});
  565. test([](auto, auto &... comp) {
  566. ((comp.x = {}), ...);
  567. });
  568. }
  569. TEST(Benchmark, IterateTenComponentsRuntime1M) {
  570. entt::registry<> registry;
  571. std::cout << "Iterating over 1000000 entities, ten components, runtime view" << std::endl;
  572. for(std::uint64_t i = 0; i < 1000000L; i++) {
  573. const auto entity = registry.create();
  574. registry.assign<position>(entity);
  575. registry.assign<velocity>(entity);
  576. registry.assign<comp<1>>(entity);
  577. registry.assign<comp<2>>(entity);
  578. registry.assign<comp<3>>(entity);
  579. registry.assign<comp<4>>(entity);
  580. registry.assign<comp<5>>(entity);
  581. registry.assign<comp<6>>(entity);
  582. registry.assign<comp<7>>(entity);
  583. registry.assign<comp<8>>(entity);
  584. }
  585. auto test = [&registry](auto func) {
  586. using component_type = typename entt::registry<>::component_type;
  587. component_type types[] = {
  588. registry.type<position>(),
  589. registry.type<velocity>(),
  590. registry.type<comp<1>>(),
  591. registry.type<comp<2>>(),
  592. registry.type<comp<3>>(),
  593. registry.type<comp<4>>(),
  594. registry.type<comp<5>>(),
  595. registry.type<comp<6>>(),
  596. registry.type<comp<7>>(),
  597. registry.type<comp<8>>()
  598. };
  599. timer timer;
  600. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  601. timer.elapsed();
  602. };
  603. test([](auto) {});
  604. test([&registry](auto entity) {
  605. registry.get<position>(entity).x = {};
  606. registry.get<velocity>(entity).x = {};
  607. registry.get<comp<1>>(entity).x = {};
  608. registry.get<comp<2>>(entity).x = {};
  609. registry.get<comp<3>>(entity).x = {};
  610. registry.get<comp<4>>(entity).x = {};
  611. registry.get<comp<5>>(entity).x = {};
  612. registry.get<comp<6>>(entity).x = {};
  613. registry.get<comp<7>>(entity).x = {};
  614. registry.get<comp<8>>(entity).x = {};
  615. });
  616. }
  617. TEST(Benchmark, IterateTenComponentsRuntime1MHalf) {
  618. entt::registry<> registry;
  619. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components, runtime view" << std::endl;
  620. for(std::uint64_t i = 0; i < 1000000L; i++) {
  621. const auto entity = registry.create();
  622. registry.assign<velocity>(entity);
  623. registry.assign<comp<1>>(entity);
  624. registry.assign<comp<2>>(entity);
  625. registry.assign<comp<3>>(entity);
  626. registry.assign<comp<4>>(entity);
  627. registry.assign<comp<5>>(entity);
  628. registry.assign<comp<6>>(entity);
  629. registry.assign<comp<7>>(entity);
  630. registry.assign<comp<8>>(entity);
  631. if(i % 2) {
  632. registry.assign<position>(entity);
  633. }
  634. }
  635. auto test = [&registry](auto func) {
  636. using component_type = typename entt::registry<>::component_type;
  637. component_type types[] = {
  638. registry.type<position>(),
  639. registry.type<velocity>(),
  640. registry.type<comp<1>>(),
  641. registry.type<comp<2>>(),
  642. registry.type<comp<3>>(),
  643. registry.type<comp<4>>(),
  644. registry.type<comp<5>>(),
  645. registry.type<comp<6>>(),
  646. registry.type<comp<7>>(),
  647. registry.type<comp<8>>()
  648. };
  649. timer timer;
  650. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  651. timer.elapsed();
  652. };
  653. test([](auto) {});
  654. test([&registry](auto entity) {
  655. registry.get<position>(entity).x = {};
  656. registry.get<velocity>(entity).x = {};
  657. registry.get<comp<1>>(entity).x = {};
  658. registry.get<comp<2>>(entity).x = {};
  659. registry.get<comp<3>>(entity).x = {};
  660. registry.get<comp<4>>(entity).x = {};
  661. registry.get<comp<5>>(entity).x = {};
  662. registry.get<comp<6>>(entity).x = {};
  663. registry.get<comp<7>>(entity).x = {};
  664. registry.get<comp<8>>(entity).x = {};
  665. });
  666. }
  667. TEST(Benchmark, IterateTenComponentsRuntime1MOne) {
  668. entt::registry<> registry;
  669. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components, runtime view" << std::endl;
  670. for(std::uint64_t i = 0; i < 1000000L; i++) {
  671. const auto entity = registry.create();
  672. registry.assign<velocity>(entity);
  673. registry.assign<comp<1>>(entity);
  674. registry.assign<comp<2>>(entity);
  675. registry.assign<comp<3>>(entity);
  676. registry.assign<comp<4>>(entity);
  677. registry.assign<comp<5>>(entity);
  678. registry.assign<comp<6>>(entity);
  679. registry.assign<comp<7>>(entity);
  680. registry.assign<comp<8>>(entity);
  681. if(i == 5000000L) {
  682. registry.assign<position>(entity);
  683. }
  684. }
  685. auto test = [&registry](auto func) {
  686. using component_type = typename entt::registry<>::component_type;
  687. component_type types[] = {
  688. registry.type<position>(),
  689. registry.type<velocity>(),
  690. registry.type<comp<1>>(),
  691. registry.type<comp<2>>(),
  692. registry.type<comp<3>>(),
  693. registry.type<comp<4>>(),
  694. registry.type<comp<5>>(),
  695. registry.type<comp<6>>(),
  696. registry.type<comp<7>>(),
  697. registry.type<comp<8>>()
  698. };
  699. timer timer;
  700. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  701. timer.elapsed();
  702. };
  703. test([](auto) {});
  704. test([&registry](auto entity) {
  705. registry.get<position>(entity).x = {};
  706. registry.get<velocity>(entity).x = {};
  707. registry.get<comp<1>>(entity).x = {};
  708. registry.get<comp<2>>(entity).x = {};
  709. registry.get<comp<3>>(entity).x = {};
  710. registry.get<comp<4>>(entity).x = {};
  711. registry.get<comp<5>>(entity).x = {};
  712. registry.get<comp<6>>(entity).x = {};
  713. registry.get<comp<7>>(entity).x = {};
  714. registry.get<comp<8>>(entity).x = {};
  715. });
  716. }
  717. TEST(Benchmark, SortSingle) {
  718. entt::registry<> registry;
  719. std::cout << "Sort 150000 entities, one component" << std::endl;
  720. for(std::uint64_t i = 0; i < 150000L; i++) {
  721. const auto entity = registry.create();
  722. registry.assign<position>(entity, i, i);
  723. }
  724. timer timer;
  725. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  726. return lhs.x < rhs.x && lhs.y < rhs.y;
  727. });
  728. timer.elapsed();
  729. }
  730. TEST(Benchmark, SortMulti) {
  731. entt::registry<> registry;
  732. std::cout << "Sort 150000 entities, two components" << std::endl;
  733. for(std::uint64_t i = 0; i < 150000L; i++) {
  734. const auto entity = registry.create();
  735. registry.assign<position>(entity, i, i);
  736. registry.assign<velocity>(entity, i, i);
  737. }
  738. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  739. return lhs.x < rhs.x && lhs.y < rhs.y;
  740. });
  741. timer timer;
  742. registry.sort<velocity, position>();
  743. timer.elapsed();
  744. }
  745. TEST(Benchmark, AlmostSortedStdSort) {
  746. entt::registry<> registry;
  747. entt::registry<>::entity_type entities[3];
  748. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  749. for(std::uint64_t i = 0; i < 150000L; i++) {
  750. const auto entity = registry.create();
  751. registry.assign<position>(entity, i, i);
  752. if(!(i % 50000)) {
  753. entities[i / 50000] = entity;
  754. }
  755. }
  756. for(std::uint64_t i = 0; i < 3; ++i) {
  757. registry.destroy(entities[i]);
  758. const auto entity = registry.create();
  759. registry.assign<position>(entity, 50000 * i, 50000 * i);
  760. }
  761. timer timer;
  762. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  763. return lhs.x > rhs.x && lhs.y > rhs.y;
  764. });
  765. timer.elapsed();
  766. }
  767. TEST(Benchmark, AlmostSortedInsertionSort) {
  768. entt::registry<> registry;
  769. entt::registry<>::entity_type entities[3];
  770. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
  771. for(std::uint64_t i = 0; i < 150000L; i++) {
  772. const auto entity = registry.create();
  773. registry.assign<position>(entity, i, i);
  774. if(!(i % 50000)) {
  775. entities[i / 50000] = entity;
  776. }
  777. }
  778. for(std::uint64_t i = 0; i < 3; ++i) {
  779. registry.destroy(entities[i]);
  780. const auto entity = registry.create();
  781. registry.assign<position>(entity, 50000 * i, 50000 * i);
  782. }
  783. timer timer;
  784. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  785. return lhs.x > rhs.x && lhs.y > rhs.y;
  786. }, entt::insertion_sort{});
  787. timer.elapsed();
  788. }