benchmark.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <chrono>
  5. #include <iterator>
  6. #include <gtest/gtest.h>
  7. #include <entt/core/type_info.hpp>
  8. #include <entt/entity/registry.hpp>
  9. struct position {
  10. std::uint64_t x;
  11. std::uint64_t y;
  12. };
  13. struct velocity {
  14. std::uint64_t x;
  15. std::uint64_t y;
  16. };
  17. template<std::size_t>
  18. struct comp { int x; };
  19. struct timer final {
  20. timer(): start{std::chrono::system_clock::now()} {}
  21. void elapsed() {
  22. auto now = std::chrono::system_clock::now();
  23. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  24. }
  25. private:
  26. std::chrono::time_point<std::chrono::system_clock> start;
  27. };
  28. template<typename Func>
  29. void pathological(Func func) {
  30. entt::registry registry;
  31. for(std::uint64_t i = 0; i < 500000L; i++) {
  32. const auto entity = registry.create();
  33. registry.emplace<position>(entity);
  34. registry.emplace<velocity>(entity);
  35. registry.emplace<comp<0>>(entity);
  36. }
  37. for(auto i = 0; i < 10; ++i) {
  38. registry.each([i = 0, &registry](const auto entity) mutable {
  39. if(!(++i % 7)) { registry.remove<position>(entity); }
  40. if(!(++i % 11)) { registry.remove<velocity>(entity); }
  41. if(!(++i % 13)) { registry.remove<comp<0>>(entity); }
  42. if(!(++i % 17)) { registry.destroy(entity); }
  43. });
  44. for(std::uint64_t j = 0; j < 50000L; j++) {
  45. const auto entity = registry.create();
  46. registry.emplace<position>(entity);
  47. registry.emplace<velocity>(entity);
  48. registry.emplace<comp<0>>(entity);
  49. }
  50. }
  51. func(registry, [](auto &... comp) {
  52. ((comp.x = {}), ...);
  53. });
  54. }
  55. TEST(Benchmark, Create) {
  56. entt::registry registry;
  57. std::cout << "Creating 1000000 entities" << std::endl;
  58. timer timer;
  59. for(std::uint64_t i = 0; i < 1000000L; i++) {
  60. registry.create();
  61. }
  62. timer.elapsed();
  63. }
  64. TEST(Benchmark, CreateMany) {
  65. entt::registry registry;
  66. std::vector<entt::entity> entities(1000000);
  67. std::cout << "Creating 1000000 entities at once" << std::endl;
  68. timer timer;
  69. registry.create(entities.begin(), entities.end());
  70. timer.elapsed();
  71. }
  72. TEST(Benchmark, CreateManyAndEmplaceComponents) {
  73. entt::registry registry;
  74. std::vector<entt::entity> entities(1000000);
  75. std::cout << "Creating 1000000 entities at once and emplace components" << std::endl;
  76. timer timer;
  77. registry.create(entities.begin(), entities.end());
  78. for(const auto entity: entities) {
  79. registry.emplace<position>(entity);
  80. registry.emplace<velocity>(entity);
  81. }
  82. timer.elapsed();
  83. }
  84. TEST(Benchmark, CreateManyWithComponents) {
  85. entt::registry registry;
  86. std::vector<entt::entity> entities(1000000);
  87. std::cout << "Creating 1000000 entities at once with components" << std::endl;
  88. timer timer;
  89. registry.create(entities.begin(), entities.end());
  90. registry.insert<position>(entities.begin(), entities.end());
  91. registry.insert<velocity>(entities.begin(), entities.end());
  92. timer.elapsed();
  93. }
  94. TEST(Benchmark, Erase) {
  95. entt::registry registry;
  96. std::vector<entt::entity> entities(1000000);
  97. std::cout << "Removing 1000000 components from their entities" << std::endl;
  98. registry.create(entities.begin(), entities.end());
  99. registry.insert<int>(entities.begin(), entities.end());
  100. timer timer;
  101. for(auto entity: registry.view<int>()) {
  102. registry.erase<int>(entity);
  103. }
  104. timer.elapsed();
  105. }
  106. TEST(Benchmark, EraseMany) {
  107. entt::registry registry;
  108. std::vector<entt::entity> entities(1000000);
  109. std::cout << "Removing 999999 components from their entities at once" << std::endl;
  110. registry.create(entities.begin(), entities.end());
  111. registry.insert<int>(entities.begin(), entities.end());
  112. timer timer;
  113. auto view = registry.view<int>();
  114. registry.erase<int>(++view.begin(), view.end());
  115. timer.elapsed();
  116. }
  117. TEST(Benchmark, Remove) {
  118. entt::registry registry;
  119. std::vector<entt::entity> entities(1000000);
  120. std::cout << "Removing 1000000 components from their entities" << std::endl;
  121. registry.create(entities.begin(), entities.end());
  122. registry.insert<int>(entities.begin(), entities.end());
  123. timer timer;
  124. for(auto entity: registry.view<int>()) {
  125. registry.remove<int>(entity);
  126. }
  127. timer.elapsed();
  128. }
  129. TEST(Benchmark, RemoveMany) {
  130. entt::registry registry;
  131. std::vector<entt::entity> entities(1000000);
  132. std::cout << "Removing 999999 components from their entities at once" << std::endl;
  133. registry.create(entities.begin(), entities.end());
  134. registry.insert<int>(entities.begin(), entities.end());
  135. timer timer;
  136. auto view = registry.view<int>();
  137. registry.remove<int>(++view.begin(), view.end());
  138. timer.elapsed();
  139. }
  140. TEST(Benchmark, Recycle) {
  141. entt::registry registry;
  142. std::vector<entt::entity> entities(1000000);
  143. std::cout << "Recycling 1000000 entities" << std::endl;
  144. registry.create(entities.begin(), entities.end());
  145. registry.each([&registry](auto entity) {
  146. registry.destroy(entity);
  147. });
  148. timer timer;
  149. for(auto next = entities.size(); next; --next) {
  150. registry.create();
  151. }
  152. timer.elapsed();
  153. }
  154. TEST(Benchmark, RecycleMany) {
  155. entt::registry registry;
  156. std::vector<entt::entity> entities(1000000);
  157. std::cout << "Recycling 1000000 entities" << std::endl;
  158. registry.create(entities.begin(), entities.end());
  159. registry.each([&registry](auto entity) {
  160. registry.destroy(entity);
  161. });
  162. timer timer;
  163. registry.create(entities.begin(), entities.end());
  164. timer.elapsed();
  165. }
  166. TEST(Benchmark, Destroy) {
  167. entt::registry registry;
  168. std::vector<entt::entity> entities(1000000);
  169. std::cout << "Destroying 1000000 entities" << std::endl;
  170. registry.create(entities.begin(), entities.end());
  171. registry.insert<int>(entities.begin(), entities.end());
  172. timer timer;
  173. for(auto entity: registry.view<int>()) {
  174. registry.destroy(entity);
  175. }
  176. timer.elapsed();
  177. }
  178. TEST(Benchmark, DestroyMany) {
  179. entt::registry registry;
  180. std::vector<entt::entity> entities(1000000);
  181. std::cout << "Destroying 1000000 entities" << std::endl;
  182. registry.create(entities.begin(), entities.end());
  183. registry.insert<int>(entities.begin(), entities.end());
  184. timer timer;
  185. auto view = registry.view<int>();
  186. registry.destroy(view.begin(), view.end());
  187. timer.elapsed();
  188. }
  189. TEST(Benchmark, IterateSingleComponent1M) {
  190. entt::registry registry;
  191. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  192. for(std::uint64_t i = 0; i < 1000000L; i++) {
  193. const auto entity = registry.create();
  194. registry.emplace<position>(entity);
  195. }
  196. auto test = [&](auto func) {
  197. timer timer;
  198. registry.view<position>().each(func);
  199. timer.elapsed();
  200. };
  201. test([](auto &... comp) {
  202. ((comp.x = {}), ...);
  203. });
  204. }
  205. TEST(Benchmark, IterateSingleComponentRuntime1M) {
  206. entt::registry registry;
  207. std::cout << "Iterating over 1000000 entities, one component, runtime view" << std::endl;
  208. for(std::uint64_t i = 0; i < 1000000L; i++) {
  209. const auto entity = registry.create();
  210. registry.emplace<position>(entity);
  211. }
  212. auto test = [&](auto func) {
  213. entt::id_type types[] = { entt::type_hash<position>::value() };
  214. timer timer;
  215. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  216. timer.elapsed();
  217. };
  218. test([&registry](auto entity) {
  219. registry.get<position>(entity).x = {};
  220. });
  221. }
  222. TEST(Benchmark, IterateTwoComponents1M) {
  223. entt::registry registry;
  224. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  225. for(std::uint64_t i = 0; i < 1000000L; i++) {
  226. const auto entity = registry.create();
  227. registry.emplace<position>(entity);
  228. registry.emplace<velocity>(entity);
  229. }
  230. auto test = [&](auto func) {
  231. timer timer;
  232. registry.view<position, velocity>().each(func);
  233. timer.elapsed();
  234. };
  235. test([](auto &... comp) {
  236. ((comp.x = {}), ...);
  237. });
  238. }
  239. TEST(Benchmark, IterateTwoComponents1MHalf) {
  240. entt::registry registry;
  241. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  242. for(std::uint64_t i = 0; i < 1000000L; i++) {
  243. const auto entity = registry.create();
  244. registry.emplace<velocity>(entity);
  245. if(i % 2) {
  246. registry.emplace<position>(entity);
  247. }
  248. }
  249. auto test = [&](auto func) {
  250. timer timer;
  251. registry.view<position, velocity>().each(func);
  252. timer.elapsed();
  253. };
  254. test([](auto &... comp) {
  255. ((comp.x = {}), ...);
  256. });
  257. }
  258. TEST(Benchmark, IterateTwoComponents1MOne) {
  259. entt::registry registry;
  260. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  261. for(std::uint64_t i = 0; i < 1000000L; i++) {
  262. const auto entity = registry.create();
  263. registry.emplace<velocity>(entity);
  264. if(i == 500000L) {
  265. registry.emplace<position>(entity);
  266. }
  267. }
  268. auto test = [&](auto func) {
  269. timer timer;
  270. registry.view<position, velocity>().each(func);
  271. timer.elapsed();
  272. };
  273. test([](auto &... comp) {
  274. ((comp.x = {}), ...);
  275. });
  276. }
  277. TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) {
  278. entt::registry registry;
  279. const auto group = registry.group<>(entt::get<position, velocity>);
  280. std::cout << "Iterating over 1000000 entities, two components, non owning group" << std::endl;
  281. for(std::uint64_t i = 0; i < 1000000L; i++) {
  282. const auto entity = registry.create();
  283. registry.emplace<position>(entity);
  284. registry.emplace<velocity>(entity);
  285. }
  286. auto test = [&](auto func) {
  287. timer timer;
  288. group.each(func);
  289. timer.elapsed();
  290. };
  291. test([](auto &... comp) {
  292. ((comp.x = {}), ...);
  293. });
  294. }
  295. TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) {
  296. entt::registry registry;
  297. const auto group = registry.group<position, velocity>();
  298. std::cout << "Iterating over 1000000 entities, two components, full owning group" << std::endl;
  299. for(std::uint64_t i = 0; i < 1000000L; i++) {
  300. const auto entity = registry.create();
  301. registry.emplace<position>(entity);
  302. registry.emplace<velocity>(entity);
  303. }
  304. auto test = [&](auto func) {
  305. timer timer;
  306. group.each(func);
  307. timer.elapsed();
  308. };
  309. test([](auto &... comp) {
  310. ((comp.x = {}), ...);
  311. });
  312. }
  313. TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) {
  314. entt::registry registry;
  315. const auto group = registry.group<position>(entt::get<velocity>);
  316. std::cout << "Iterating over 1000000 entities, two components, partial owning group" << std::endl;
  317. for(std::uint64_t i = 0; i < 1000000L; i++) {
  318. const auto entity = registry.create();
  319. registry.emplace<position>(entity);
  320. registry.emplace<velocity>(entity);
  321. }
  322. auto test = [&](auto func) {
  323. timer timer;
  324. group.each(func);
  325. timer.elapsed();
  326. };
  327. test([](auto &... comp) {
  328. ((comp.x = {}), ...);
  329. });
  330. }
  331. TEST(Benchmark, IterateTwoComponentsRuntime1M) {
  332. entt::registry registry;
  333. std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
  334. for(std::uint64_t i = 0; i < 1000000L; i++) {
  335. const auto entity = registry.create();
  336. registry.emplace<position>(entity);
  337. registry.emplace<velocity>(entity);
  338. }
  339. auto test = [&](auto func) {
  340. entt::id_type types[] = {
  341. entt::type_hash<position>::value(),
  342. entt::type_hash<velocity>::value()
  343. };
  344. timer timer;
  345. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  346. timer.elapsed();
  347. };
  348. test([&registry](auto entity) {
  349. registry.get<position>(entity).x = {};
  350. registry.get<velocity>(entity).x = {};
  351. });
  352. }
  353. TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
  354. entt::registry registry;
  355. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
  356. for(std::uint64_t i = 0; i < 1000000L; i++) {
  357. const auto entity = registry.create();
  358. registry.emplace<velocity>(entity);
  359. if(i % 2) {
  360. registry.emplace<position>(entity);
  361. }
  362. }
  363. auto test = [&](auto func) {
  364. entt::id_type types[] = {
  365. entt::type_hash<position>::value(),
  366. entt::type_hash<velocity>::value()
  367. };
  368. timer timer;
  369. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  370. timer.elapsed();
  371. };
  372. test([&registry](auto entity) {
  373. registry.get<position>(entity).x = {};
  374. registry.get<velocity>(entity).x = {};
  375. });
  376. }
  377. TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
  378. entt::registry registry;
  379. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
  380. for(std::uint64_t i = 0; i < 1000000L; i++) {
  381. const auto entity = registry.create();
  382. registry.emplace<velocity>(entity);
  383. if(i == 500000L) {
  384. registry.emplace<position>(entity);
  385. }
  386. }
  387. auto test = [&](auto func) {
  388. entt::id_type types[] = {
  389. entt::type_hash<position>::value(),
  390. entt::type_hash<velocity>::value()
  391. };
  392. timer timer;
  393. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  394. timer.elapsed();
  395. };
  396. test([&registry](auto entity) {
  397. registry.get<position>(entity).x = {};
  398. registry.get<velocity>(entity).x = {};
  399. });
  400. }
  401. TEST(Benchmark, IterateThreeComponents1M) {
  402. entt::registry registry;
  403. std::cout << "Iterating over 1000000 entities, three components" << std::endl;
  404. for(std::uint64_t i = 0; i < 1000000L; i++) {
  405. const auto entity = registry.create();
  406. registry.emplace<position>(entity);
  407. registry.emplace<velocity>(entity);
  408. registry.emplace<comp<0>>(entity);
  409. }
  410. auto test = [&](auto func) {
  411. timer timer;
  412. registry.view<position, velocity, comp<0>>().each(func);
  413. timer.elapsed();
  414. };
  415. test([](auto &... comp) {
  416. ((comp.x = {}), ...);
  417. });
  418. }
  419. TEST(Benchmark, IterateThreeComponents1MHalf) {
  420. entt::registry registry;
  421. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components" << std::endl;
  422. for(std::uint64_t i = 0; i < 1000000L; i++) {
  423. const auto entity = registry.create();
  424. registry.emplace<velocity>(entity);
  425. registry.emplace<comp<0>>(entity);
  426. if(i % 2) {
  427. registry.emplace<position>(entity);
  428. }
  429. }
  430. auto test = [&](auto func) {
  431. timer timer;
  432. registry.view<position, velocity, comp<0>>().each(func);
  433. timer.elapsed();
  434. };
  435. test([](auto &... comp) {
  436. ((comp.x = {}), ...);
  437. });
  438. }
  439. TEST(Benchmark, IterateThreeComponents1MOne) {
  440. entt::registry registry;
  441. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components" << std::endl;
  442. for(std::uint64_t i = 0; i < 1000000L; i++) {
  443. const auto entity = registry.create();
  444. registry.emplace<velocity>(entity);
  445. registry.emplace<comp<0>>(entity);
  446. if(i == 500000L) {
  447. registry.emplace<position>(entity);
  448. }
  449. }
  450. auto test = [&](auto func) {
  451. timer timer;
  452. registry.view<position, velocity, comp<0>>().each(func);
  453. timer.elapsed();
  454. };
  455. test([](auto &... comp) {
  456. ((comp.x = {}), ...);
  457. });
  458. }
  459. TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) {
  460. entt::registry registry;
  461. const auto group = registry.group<>(entt::get<position, velocity, comp<0>>);
  462. std::cout << "Iterating over 1000000 entities, three components, non owning group" << std::endl;
  463. for(std::uint64_t i = 0; i < 1000000L; i++) {
  464. const auto entity = registry.create();
  465. registry.emplace<position>(entity);
  466. registry.emplace<velocity>(entity);
  467. registry.emplace<comp<0>>(entity);
  468. }
  469. auto test = [&](auto func) {
  470. timer timer;
  471. group.each(func);
  472. timer.elapsed();
  473. };
  474. test([](auto &... comp) {
  475. ((comp.x = {}), ...);
  476. });
  477. }
  478. TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) {
  479. entt::registry registry;
  480. const auto group = registry.group<position, velocity, comp<0>>();
  481. std::cout << "Iterating over 1000000 entities, three components, full owning group" << std::endl;
  482. for(std::uint64_t i = 0; i < 1000000L; i++) {
  483. const auto entity = registry.create();
  484. registry.emplace<position>(entity);
  485. registry.emplace<velocity>(entity);
  486. registry.emplace<comp<0>>(entity);
  487. }
  488. auto test = [&](auto func) {
  489. timer timer;
  490. group.each(func);
  491. timer.elapsed();
  492. };
  493. test([](auto &... comp) {
  494. ((comp.x = {}), ...);
  495. });
  496. }
  497. TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) {
  498. entt::registry registry;
  499. const auto group = registry.group<position, velocity>(entt::get<comp<0>>);
  500. std::cout << "Iterating over 1000000 entities, three components, partial owning group" << std::endl;
  501. for(std::uint64_t i = 0; i < 1000000L; i++) {
  502. const auto entity = registry.create();
  503. registry.emplace<position>(entity);
  504. registry.emplace<velocity>(entity);
  505. registry.emplace<comp<0>>(entity);
  506. }
  507. auto test = [&](auto func) {
  508. timer timer;
  509. group.each(func);
  510. timer.elapsed();
  511. };
  512. test([](auto &... comp) {
  513. ((comp.x = {}), ...);
  514. });
  515. }
  516. TEST(Benchmark, IterateThreeComponentsRuntime1M) {
  517. entt::registry registry;
  518. std::cout << "Iterating over 1000000 entities, three components, runtime view" << std::endl;
  519. for(std::uint64_t i = 0; i < 1000000L; i++) {
  520. const auto entity = registry.create();
  521. registry.emplace<position>(entity);
  522. registry.emplace<velocity>(entity);
  523. registry.emplace<comp<0>>(entity);
  524. }
  525. auto test = [&](auto func) {
  526. entt::id_type types[] = {
  527. entt::type_hash<position>::value(),
  528. entt::type_hash<velocity>::value(),
  529. entt::type_hash<comp<0>>::value()
  530. };
  531. timer timer;
  532. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  533. timer.elapsed();
  534. };
  535. test([&registry](auto entity) {
  536. registry.get<position>(entity).x = {};
  537. registry.get<velocity>(entity).x = {};
  538. registry.get<comp<0>>(entity).x = {};
  539. });
  540. }
  541. TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) {
  542. entt::registry registry;
  543. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components, runtime view" << std::endl;
  544. for(std::uint64_t i = 0; i < 1000000L; i++) {
  545. const auto entity = registry.create();
  546. registry.emplace<velocity>(entity);
  547. registry.emplace<comp<0>>(entity);
  548. if(i % 2) {
  549. registry.emplace<position>(entity);
  550. }
  551. }
  552. auto test = [&](auto func) {
  553. entt::id_type types[] = {
  554. entt::type_hash<position>::value(),
  555. entt::type_hash<velocity>::value(),
  556. entt::type_hash<comp<0>>::value()
  557. };
  558. timer timer;
  559. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  560. timer.elapsed();
  561. };
  562. test([&registry](auto entity) {
  563. registry.get<position>(entity).x = {};
  564. registry.get<velocity>(entity).x = {};
  565. registry.get<comp<0>>(entity).x = {};
  566. });
  567. }
  568. TEST(Benchmark, IterateThreeComponentsRuntime1MOne) {
  569. entt::registry registry;
  570. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components, runtime view" << std::endl;
  571. for(std::uint64_t i = 0; i < 1000000L; i++) {
  572. const auto entity = registry.create();
  573. registry.emplace<velocity>(entity);
  574. registry.emplace<comp<0>>(entity);
  575. if(i == 500000L) {
  576. registry.emplace<position>(entity);
  577. }
  578. }
  579. auto test = [&](auto func) {
  580. entt::id_type types[] = {
  581. entt::type_hash<position>::value(),
  582. entt::type_hash<velocity>::value(),
  583. entt::type_hash<comp<0>>::value()
  584. };
  585. timer timer;
  586. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  587. timer.elapsed();
  588. };
  589. test([&registry](auto entity) {
  590. registry.get<position>(entity).x = {};
  591. registry.get<velocity>(entity).x = {};
  592. registry.get<comp<0>>(entity).x = {};
  593. });
  594. }
  595. TEST(Benchmark, IterateFiveComponents1M) {
  596. entt::registry registry;
  597. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  598. for(std::uint64_t i = 0; i < 1000000L; i++) {
  599. const auto entity = registry.create();
  600. registry.emplace<position>(entity);
  601. registry.emplace<velocity>(entity);
  602. registry.emplace<comp<0>>(entity);
  603. registry.emplace<comp<1>>(entity);
  604. registry.emplace<comp<2>>(entity);
  605. }
  606. auto test = [&](auto func) {
  607. timer timer;
  608. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  609. timer.elapsed();
  610. };
  611. test([](auto &... comp) {
  612. ((comp.x = {}), ...);
  613. });
  614. }
  615. TEST(Benchmark, IterateFiveComponents1MHalf) {
  616. entt::registry registry;
  617. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  618. for(std::uint64_t i = 0; i < 1000000L; i++) {
  619. const auto entity = registry.create();
  620. registry.emplace<velocity>(entity);
  621. registry.emplace<comp<0>>(entity);
  622. registry.emplace<comp<1>>(entity);
  623. registry.emplace<comp<2>>(entity);
  624. if(i % 2) {
  625. registry.emplace<position>(entity);
  626. }
  627. }
  628. auto test = [&](auto func) {
  629. timer timer;
  630. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  631. timer.elapsed();
  632. };
  633. test([](auto &... comp) {
  634. ((comp.x = {}), ...);
  635. });
  636. }
  637. TEST(Benchmark, IterateFiveComponents1MOne) {
  638. entt::registry registry;
  639. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  640. for(std::uint64_t i = 0; i < 1000000L; i++) {
  641. const auto entity = registry.create();
  642. registry.emplace<velocity>(entity);
  643. registry.emplace<comp<0>>(entity);
  644. registry.emplace<comp<1>>(entity);
  645. registry.emplace<comp<2>>(entity);
  646. if(i == 500000L) {
  647. registry.emplace<position>(entity);
  648. }
  649. }
  650. auto test = [&](auto func) {
  651. timer timer;
  652. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  653. timer.elapsed();
  654. };
  655. test([](auto &... comp) {
  656. ((comp.x = {}), ...);
  657. });
  658. }
  659. TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) {
  660. entt::registry registry;
  661. const auto group = registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>);
  662. std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl;
  663. for(std::uint64_t i = 0; i < 1000000L; i++) {
  664. const auto entity = registry.create();
  665. registry.emplace<position>(entity);
  666. registry.emplace<velocity>(entity);
  667. registry.emplace<comp<0>>(entity);
  668. registry.emplace<comp<1>>(entity);
  669. registry.emplace<comp<2>>(entity);
  670. }
  671. auto test = [&](auto func) {
  672. timer timer;
  673. group.each(func);
  674. timer.elapsed();
  675. };
  676. test([](auto &... comp) {
  677. ((comp.x = {}), ...);
  678. });
  679. }
  680. TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) {
  681. entt::registry registry;
  682. const auto group = registry.group<position, velocity, comp<0>, comp<1>, comp<2>>();
  683. std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl;
  684. for(std::uint64_t i = 0; i < 1000000L; i++) {
  685. const auto entity = registry.create();
  686. registry.emplace<position>(entity);
  687. registry.emplace<velocity>(entity);
  688. registry.emplace<comp<0>>(entity);
  689. registry.emplace<comp<1>>(entity);
  690. registry.emplace<comp<2>>(entity);
  691. }
  692. auto test = [&](auto func) {
  693. timer timer;
  694. group.each(func);
  695. timer.elapsed();
  696. };
  697. test([](auto &... comp) {
  698. ((comp.x = {}), ...);
  699. });
  700. }
  701. TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) {
  702. entt::registry registry;
  703. const auto group = registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>);
  704. std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl;
  705. for(std::uint64_t i = 0; i < 1000000L; i++) {
  706. const auto entity = registry.create();
  707. registry.emplace<position>(entity);
  708. registry.emplace<velocity>(entity);
  709. registry.emplace<comp<0>>(entity);
  710. registry.emplace<comp<1>>(entity);
  711. registry.emplace<comp<2>>(entity);
  712. }
  713. auto test = [&](auto func) {
  714. timer timer;
  715. group.each(func);
  716. timer.elapsed();
  717. };
  718. test([](auto &... comp) {
  719. ((comp.x = {}), ...);
  720. });
  721. }
  722. TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) {
  723. entt::registry registry;
  724. const auto group = registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>);
  725. std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) owning group" << std::endl;
  726. for(std::uint64_t i = 0; i < 1000000L; i++) {
  727. const auto entity = registry.create();
  728. registry.emplace<position>(entity);
  729. registry.emplace<velocity>(entity);
  730. registry.emplace<comp<0>>(entity);
  731. registry.emplace<comp<1>>(entity);
  732. registry.emplace<comp<2>>(entity);
  733. }
  734. auto test = [&](auto func) {
  735. timer timer;
  736. group.each(func);
  737. timer.elapsed();
  738. };
  739. test([](auto &... comp) {
  740. ((comp.x = {}), ...);
  741. });
  742. }
  743. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  744. entt::registry registry;
  745. std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
  746. for(std::uint64_t i = 0; i < 1000000L; i++) {
  747. const auto entity = registry.create();
  748. registry.emplace<position>(entity);
  749. registry.emplace<velocity>(entity);
  750. registry.emplace<comp<0>>(entity);
  751. registry.emplace<comp<1>>(entity);
  752. registry.emplace<comp<2>>(entity);
  753. }
  754. auto test = [&](auto func) {
  755. entt::id_type types[] = {
  756. entt::type_hash<position>::value(),
  757. entt::type_hash<velocity>::value(),
  758. entt::type_hash<comp<0>>::value(),
  759. entt::type_hash<comp<1>>::value(),
  760. entt::type_hash<comp<2>>::value()
  761. };
  762. timer timer;
  763. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  764. timer.elapsed();
  765. };
  766. test([&registry](auto entity) {
  767. registry.get<position>(entity).x = {};
  768. registry.get<velocity>(entity).x = {};
  769. registry.get<comp<0>>(entity).x = {};
  770. registry.get<comp<1>>(entity).x = {};
  771. registry.get<comp<2>>(entity).x = {};
  772. });
  773. }
  774. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  775. entt::registry registry;
  776. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  777. for(std::uint64_t i = 0; i < 1000000L; i++) {
  778. const auto entity = registry.create();
  779. registry.emplace<velocity>(entity);
  780. registry.emplace<comp<0>>(entity);
  781. registry.emplace<comp<1>>(entity);
  782. registry.emplace<comp<2>>(entity);
  783. if(i % 2) {
  784. registry.emplace<position>(entity);
  785. }
  786. }
  787. auto test = [&](auto func) {
  788. entt::id_type types[] = {
  789. entt::type_hash<position>::value(),
  790. entt::type_hash<velocity>::value(),
  791. entt::type_hash<comp<0>>::value(),
  792. entt::type_hash<comp<1>>::value(),
  793. entt::type_hash<comp<2>>::value()
  794. };
  795. timer timer;
  796. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  797. timer.elapsed();
  798. };
  799. test([&registry](auto entity) {
  800. registry.get<position>(entity).x = {};
  801. registry.get<velocity>(entity).x = {};
  802. registry.get<comp<0>>(entity).x = {};
  803. registry.get<comp<1>>(entity).x = {};
  804. registry.get<comp<2>>(entity).x = {};
  805. });
  806. }
  807. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  808. entt::registry registry;
  809. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  810. for(std::uint64_t i = 0; i < 1000000L; i++) {
  811. const auto entity = registry.create();
  812. registry.emplace<velocity>(entity);
  813. registry.emplace<comp<0>>(entity);
  814. registry.emplace<comp<1>>(entity);
  815. registry.emplace<comp<2>>(entity);
  816. if(i == 500000L) {
  817. registry.emplace<position>(entity);
  818. }
  819. }
  820. auto test = [&](auto func) {
  821. entt::id_type types[] = {
  822. entt::type_hash<position>::value(),
  823. entt::type_hash<velocity>::value(),
  824. entt::type_hash<comp<0>>::value(),
  825. entt::type_hash<comp<1>>::value(),
  826. entt::type_hash<comp<2>>::value()
  827. };
  828. timer timer;
  829. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  830. timer.elapsed();
  831. };
  832. test([&registry](auto entity) {
  833. registry.get<position>(entity).x = {};
  834. registry.get<velocity>(entity).x = {};
  835. registry.get<comp<0>>(entity).x = {};
  836. registry.get<comp<1>>(entity).x = {};
  837. registry.get<comp<2>>(entity).x = {};
  838. });
  839. }
  840. TEST(Benchmark, IteratePathological) {
  841. std::cout << "Pathological case" << std::endl;
  842. pathological([](auto &registry, auto func) {
  843. timer timer;
  844. registry.template view<position, velocity, comp<0>>().each(func);
  845. timer.elapsed();
  846. });
  847. }
  848. TEST(Benchmark, IteratePathologicalNonOwningGroup) {
  849. std::cout << "Pathological case (non-owning group)" << std::endl;
  850. pathological([](auto &registry, auto func) {
  851. auto group = registry.template group<>(entt::get<position, velocity, comp<0>>);
  852. timer timer;
  853. group.each(func);
  854. timer.elapsed();
  855. });
  856. }
  857. TEST(Benchmark, IteratePathologicalFullOwningGroup) {
  858. std::cout << "Pathological case (full-owning group)" << std::endl;
  859. pathological([](auto &registry, auto func) {
  860. auto group = registry.template group<position, velocity, comp<0>>();
  861. timer timer;
  862. group.each(func);
  863. timer.elapsed();
  864. });
  865. }
  866. TEST(Benchmark, IteratePathologicalPartialOwningGroup) {
  867. std::cout << "Pathological case (partial-owning group)" << std::endl;
  868. pathological([](auto &registry, auto func) {
  869. auto group = registry.template group<position, velocity>(entt::get<comp<0>>);
  870. timer timer;
  871. group.each(func);
  872. timer.elapsed();
  873. });
  874. }
  875. TEST(Benchmark, SortSingle) {
  876. entt::registry registry;
  877. std::cout << "Sort 150000 entities, one component" << std::endl;
  878. for(std::uint64_t i = 0; i < 150000L; i++) {
  879. const auto entity = registry.create();
  880. registry.emplace<position>(entity, i, i);
  881. }
  882. timer timer;
  883. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  884. return lhs.x < rhs.x && lhs.y < rhs.y;
  885. });
  886. timer.elapsed();
  887. }
  888. TEST(Benchmark, SortMulti) {
  889. entt::registry registry;
  890. std::cout << "Sort 150000 entities, two components" << std::endl;
  891. for(std::uint64_t i = 0; i < 150000L; i++) {
  892. const auto entity = registry.create();
  893. registry.emplace<position>(entity, i, i);
  894. registry.emplace<velocity>(entity, i, i);
  895. }
  896. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  897. return lhs.x < rhs.x && lhs.y < rhs.y;
  898. });
  899. timer timer;
  900. registry.sort<velocity, position>();
  901. timer.elapsed();
  902. }
  903. TEST(Benchmark, AlmostSortedStdSort) {
  904. entt::registry registry;
  905. entt::entity entities[3]{};
  906. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  907. for(std::uint64_t i = 0; i < 150000L; i++) {
  908. const auto entity = registry.create();
  909. registry.emplace<position>(entity, i, i);
  910. if(!(i % 50000)) {
  911. entities[i / 50000] = entity;
  912. }
  913. }
  914. for(std::uint64_t i = 0; i < 3; ++i) {
  915. registry.destroy(entities[i]);
  916. const auto entity = registry.create();
  917. registry.emplace<position>(entity, 50000 * i, 50000 * i);
  918. }
  919. timer timer;
  920. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  921. return lhs.x > rhs.x && lhs.y > rhs.y;
  922. });
  923. timer.elapsed();
  924. }
  925. TEST(Benchmark, AlmostSortedInsertionSort) {
  926. entt::registry registry;
  927. entt::entity entities[3]{};
  928. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
  929. for(std::uint64_t i = 0; i < 150000L; i++) {
  930. const auto entity = registry.create();
  931. registry.emplace<position>(entity, i, i);
  932. if(!(i % 50000)) {
  933. entities[i / 50000] = entity;
  934. }
  935. }
  936. for(std::uint64_t i = 0; i < 3; ++i) {
  937. registry.destroy(entities[i]);
  938. const auto entity = registry.create();
  939. registry.emplace<position>(entity, 50000 * i, 50000 * i);
  940. }
  941. timer timer;
  942. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  943. return lhs.x > rhs.x && lhs.y > rhs.y;
  944. }, entt::insertion_sort{});
  945. timer.elapsed();
  946. }