benchmark.cpp 36 KB

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