benchmark.cpp 31 KB

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