benchmark.cpp 31 KB

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