benchmark.cpp 37 KB

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