benchmark.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <chrono>
  5. #include <iterator>
  6. #include <gtest/gtest.h>
  7. #include <entt/entity/registry.hpp>
  8. struct position {
  9. std::uint64_t x;
  10. std::uint64_t y;
  11. };
  12. struct velocity {
  13. std::uint64_t x;
  14. std::uint64_t y;
  15. };
  16. template<std::size_t>
  17. struct comp { int x; };
  18. struct timer final {
  19. timer(): start{std::chrono::system_clock::now()} {}
  20. void elapsed() {
  21. auto now = std::chrono::system_clock::now();
  22. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  23. }
  24. private:
  25. std::chrono::time_point<std::chrono::system_clock> start;
  26. };
  27. TEST(Benchmark, Construct) {
  28. entt::registry<> registry;
  29. std::cout << "Constructing 1000000 entities" << std::endl;
  30. timer timer;
  31. for(std::uint64_t i = 0; i < 1000000L; i++) {
  32. registry.create();
  33. }
  34. timer.elapsed();
  35. }
  36. TEST(Benchmark, ConstructMany) {
  37. entt::registry<> registry;
  38. std::vector<entt::registry<>::entity_type> entities(1000000);
  39. std::cout << "Constructing 1000000 entities at once" << std::endl;
  40. timer timer;
  41. registry.create(entities.begin(), entities.end());
  42. timer.elapsed();
  43. }
  44. TEST(Benchmark, Destroy) {
  45. entt::registry<> registry;
  46. std::cout << "Destroying 1000000 entities" << std::endl;
  47. for(std::uint64_t i = 0; i < 1000000L; i++) {
  48. registry.create();
  49. }
  50. timer timer;
  51. registry.each([&registry](auto entity) {
  52. registry.destroy(entity);
  53. });
  54. timer.elapsed();
  55. }
  56. TEST(Benchmark, IterateCreateDeleteSingleComponent) {
  57. entt::registry<> registry;
  58. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  59. timer timer;
  60. auto view = registry.view<position>();
  61. for(int i = 0; i < 10000; i++) {
  62. for(int j = 0; j < 10000; j++) {
  63. const auto entity = registry.create();
  64. registry.assign<position>(entity);
  65. }
  66. for(auto entity: view) {
  67. if(rand() % 2 == 0) {
  68. registry.destroy(entity);
  69. }
  70. }
  71. }
  72. timer.elapsed();
  73. }
  74. TEST(Benchmark, IterateSingleComponent1M) {
  75. entt::registry<> registry;
  76. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  77. for(std::uint64_t i = 0; i < 1000000L; i++) {
  78. const auto entity = registry.create();
  79. registry.assign<position>(entity);
  80. }
  81. auto test = [&registry](auto func) {
  82. timer timer;
  83. registry.view<position>().each(func);
  84. timer.elapsed();
  85. };
  86. test([](const auto &...) {});
  87. test([](auto &... comp) {
  88. ((comp.x = {}), ...);
  89. });
  90. }
  91. TEST(Benchmark, IterateSingleComponentRuntime1M) {
  92. entt::registry<> registry;
  93. std::cout << "Iterating over 1000000 entities, one component, runtime view" << std::endl;
  94. for(std::uint64_t i = 0; i < 1000000L; i++) {
  95. const auto entity = registry.create();
  96. registry.assign<position>(entity);
  97. }
  98. auto test = [&registry](auto func) {
  99. using component_type = typename entt::registry<>::component_type;
  100. component_type types[] = { registry.type<position>() };
  101. timer timer;
  102. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  103. timer.elapsed();
  104. };
  105. test([](auto) {});
  106. test([&registry](auto entity) {
  107. registry.get<position>(entity).x = {};
  108. });
  109. }
  110. TEST(Benchmark, IterateTwoComponents1M) {
  111. entt::registry<> registry;
  112. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  113. for(std::uint64_t i = 0; i < 1000000L; i++) {
  114. const auto entity = registry.create();
  115. registry.assign<position>(entity);
  116. registry.assign<velocity>(entity);
  117. }
  118. auto test = [&registry](auto func) {
  119. timer timer;
  120. registry.view<position, velocity>().each(func);
  121. timer.elapsed();
  122. };
  123. test([](const auto &...) {});
  124. test([](auto &... comp) {
  125. ((comp.x = {}), ...);
  126. });
  127. }
  128. TEST(Benchmark, IterateTwoComponents1MHalf) {
  129. entt::registry<> registry;
  130. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  131. for(std::uint64_t i = 0; i < 1000000L; i++) {
  132. const auto entity = registry.create();
  133. registry.assign<velocity>(entity);
  134. if(i % 2) {
  135. registry.assign<position>(entity);
  136. }
  137. }
  138. auto test = [&registry](auto func) {
  139. timer timer;
  140. registry.view<position, velocity>().each(func);
  141. timer.elapsed();
  142. };
  143. test([](const auto &...) {});
  144. test([](auto &... comp) {
  145. ((comp.x = {}), ...);
  146. });
  147. }
  148. TEST(Benchmark, IterateTwoComponents1MOne) {
  149. entt::registry<> registry;
  150. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  151. for(std::uint64_t i = 0; i < 1000000L; i++) {
  152. const auto entity = registry.create();
  153. registry.assign<velocity>(entity);
  154. if(i == 5000000L) {
  155. registry.assign<position>(entity);
  156. }
  157. }
  158. auto test = [&registry](auto func) {
  159. timer timer;
  160. registry.view<position, velocity>().each(func);
  161. timer.elapsed();
  162. };
  163. test([](const auto &...) {});
  164. test([](auto &... comp) {
  165. ((comp.x = {}), ...);
  166. });
  167. }
  168. TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) {
  169. entt::registry<> registry;
  170. registry.group<>(entt::get<position, velocity>);
  171. std::cout << "Iterating over 1000000 entities, two components, non owning group" << std::endl;
  172. for(std::uint64_t i = 0; i < 1000000L; i++) {
  173. const auto entity = registry.create();
  174. registry.assign<position>(entity);
  175. registry.assign<velocity>(entity);
  176. }
  177. auto test = [&registry](auto func) {
  178. timer timer;
  179. registry.group<>(entt::get<position, velocity>).each(func);
  180. timer.elapsed();
  181. };
  182. test([](const auto &...) {});
  183. test([](auto &... comp) {
  184. ((comp.x = {}), ...);
  185. });
  186. }
  187. TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) {
  188. entt::registry<> registry;
  189. registry.group<position, velocity>();
  190. std::cout << "Iterating over 1000000 entities, two components, full owning group" << std::endl;
  191. for(std::uint64_t i = 0; i < 1000000L; i++) {
  192. const auto entity = registry.create();
  193. registry.assign<position>(entity);
  194. registry.assign<velocity>(entity);
  195. }
  196. auto test = [&registry](auto func) {
  197. timer timer;
  198. registry.group<position, velocity>().each(func);
  199. timer.elapsed();
  200. };
  201. test([](const auto &...) {});
  202. test([](auto &... comp) {
  203. ((comp.x = {}), ...);
  204. });
  205. }
  206. TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) {
  207. entt::registry<> registry;
  208. registry.group<position>(entt::get<velocity>);
  209. std::cout << "Iterating over 1000000 entities, two components, partial owning group" << std::endl;
  210. for(std::uint64_t i = 0; i < 1000000L; i++) {
  211. const auto entity = registry.create();
  212. registry.assign<position>(entity);
  213. registry.assign<velocity>(entity);
  214. }
  215. auto test = [&registry](auto func) {
  216. timer timer;
  217. registry.group<position>(entt::get<velocity>).each(func);
  218. timer.elapsed();
  219. };
  220. test([](const auto &...) {});
  221. test([](auto &... comp) {
  222. ((comp.x = {}), ...);
  223. });
  224. }
  225. TEST(Benchmark, IterateTwoComponentsRuntime1M) {
  226. entt::registry<> registry;
  227. std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
  228. for(std::uint64_t i = 0; i < 1000000L; i++) {
  229. const auto entity = registry.create();
  230. registry.assign<position>(entity);
  231. registry.assign<velocity>(entity);
  232. }
  233. auto test = [&registry](auto func) {
  234. using component_type = typename entt::registry<>::component_type;
  235. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  236. timer timer;
  237. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  238. timer.elapsed();
  239. };
  240. test([](auto) {});
  241. test([&registry](auto entity) {
  242. registry.get<position>(entity).x = {};
  243. registry.get<velocity>(entity).x = {};
  244. });
  245. }
  246. TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
  247. entt::registry<> registry;
  248. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
  249. for(std::uint64_t i = 0; i < 1000000L; i++) {
  250. const auto entity = registry.create();
  251. registry.assign<velocity>(entity);
  252. if(i % 2) {
  253. registry.assign<position>(entity);
  254. }
  255. }
  256. auto test = [&registry](auto func) {
  257. using component_type = typename entt::registry<>::component_type;
  258. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  259. timer timer;
  260. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  261. timer.elapsed();
  262. };
  263. test([](auto) {});
  264. test([&registry](auto entity) {
  265. registry.get<position>(entity).x = {};
  266. registry.get<velocity>(entity).x = {};
  267. });
  268. }
  269. TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
  270. entt::registry<> registry;
  271. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
  272. for(std::uint64_t i = 0; i < 1000000L; i++) {
  273. const auto entity = registry.create();
  274. registry.assign<velocity>(entity);
  275. if(i == 5000000L) {
  276. registry.assign<position>(entity);
  277. }
  278. }
  279. auto test = [&registry](auto func) {
  280. using component_type = typename entt::registry<>::component_type;
  281. component_type types[] = { registry.type<position>(), registry.type<velocity>() };
  282. timer timer;
  283. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  284. timer.elapsed();
  285. };
  286. test([](auto) {});
  287. test([&registry](auto entity) {
  288. registry.get<position>(entity).x = {};
  289. registry.get<velocity>(entity).x = {};
  290. });
  291. }
  292. TEST(Benchmark, IterateFiveComponents1M) {
  293. entt::registry<> registry;
  294. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  295. for(std::uint64_t i = 0; i < 1000000L; i++) {
  296. const auto entity = registry.create();
  297. registry.assign<position>(entity);
  298. registry.assign<velocity>(entity);
  299. registry.assign<comp<0>>(entity);
  300. registry.assign<comp<1>>(entity);
  301. registry.assign<comp<2>>(entity);
  302. }
  303. auto test = [&registry](auto func) {
  304. timer timer;
  305. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  306. timer.elapsed();
  307. };
  308. test([](const auto &...) {});
  309. test([](auto &... comp) {
  310. ((comp.x = {}), ...);
  311. });
  312. }
  313. TEST(Benchmark, IterateFiveComponents1MHalf) {
  314. entt::registry<> registry;
  315. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  316. for(std::uint64_t i = 0; i < 1000000L; i++) {
  317. const auto entity = registry.create();
  318. registry.assign<velocity>(entity);
  319. registry.assign<comp<0>>(entity);
  320. registry.assign<comp<1>>(entity);
  321. registry.assign<comp<2>>(entity);
  322. if(i % 2) {
  323. registry.assign<position>(entity);
  324. }
  325. }
  326. auto test = [&registry](auto func) {
  327. timer timer;
  328. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  329. timer.elapsed();
  330. };
  331. test([](const auto &...) {});
  332. test([](auto &... comp) {
  333. ((comp.x = {}), ...);
  334. });
  335. }
  336. TEST(Benchmark, IterateFiveComponents1MOne) {
  337. entt::registry<> registry;
  338. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  339. for(std::uint64_t i = 0; i < 1000000L; i++) {
  340. const auto entity = registry.create();
  341. registry.assign<velocity>(entity);
  342. registry.assign<comp<0>>(entity);
  343. registry.assign<comp<1>>(entity);
  344. registry.assign<comp<2>>(entity);
  345. if(i == 5000000L) {
  346. registry.assign<position>(entity);
  347. }
  348. }
  349. auto test = [&registry](auto func) {
  350. timer timer;
  351. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  352. timer.elapsed();
  353. };
  354. test([](const auto &...) {});
  355. test([](auto &... comp) {
  356. ((comp.x = {}), ...);
  357. });
  358. }
  359. TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) {
  360. entt::registry<> registry;
  361. registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>);
  362. std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl;
  363. for(std::uint64_t i = 0; i < 1000000L; i++) {
  364. const auto entity = registry.create();
  365. registry.assign<position>(entity);
  366. registry.assign<velocity>(entity);
  367. registry.assign<comp<0>>(entity);
  368. registry.assign<comp<1>>(entity);
  369. registry.assign<comp<2>>(entity);
  370. }
  371. auto test = [&registry](auto func) {
  372. timer timer;
  373. registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>).each(func);
  374. timer.elapsed();
  375. };
  376. test([](const auto &...) {});
  377. test([](auto &... comp) {
  378. ((comp.x = {}), ...);
  379. });
  380. }
  381. TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) {
  382. entt::registry<> registry;
  383. registry.group<position, velocity, comp<0>, comp<1>, comp<2>>();
  384. std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl;
  385. for(std::uint64_t i = 0; i < 1000000L; i++) {
  386. const auto entity = registry.create();
  387. registry.assign<position>(entity);
  388. registry.assign<velocity>(entity);
  389. registry.assign<comp<0>>(entity);
  390. registry.assign<comp<1>>(entity);
  391. registry.assign<comp<2>>(entity);
  392. }
  393. auto test = [&registry](auto func) {
  394. timer timer;
  395. registry.group<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  396. timer.elapsed();
  397. };
  398. test([](const auto &...) {});
  399. test([](auto &... comp) {
  400. ((comp.x = {}), ...);
  401. });
  402. }
  403. TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) {
  404. entt::registry<> registry;
  405. registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>);
  406. std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl;
  407. for(std::uint64_t i = 0; i < 1000000L; i++) {
  408. const auto entity = registry.create();
  409. registry.assign<position>(entity);
  410. registry.assign<velocity>(entity);
  411. registry.assign<comp<0>>(entity);
  412. registry.assign<comp<1>>(entity);
  413. registry.assign<comp<2>>(entity);
  414. }
  415. auto test = [&registry](auto func) {
  416. timer timer;
  417. registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>).each(func);
  418. timer.elapsed();
  419. };
  420. test([](const auto &...) {});
  421. test([](auto &... comp) {
  422. ((comp.x = {}), ...);
  423. });
  424. }
  425. TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) {
  426. entt::registry<> registry;
  427. registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>);
  428. std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) owning group" << std::endl;
  429. for(std::uint64_t i = 0; i < 1000000L; i++) {
  430. const auto entity = registry.create();
  431. registry.assign<position>(entity);
  432. registry.assign<velocity>(entity);
  433. registry.assign<comp<0>>(entity);
  434. registry.assign<comp<1>>(entity);
  435. registry.assign<comp<2>>(entity);
  436. }
  437. auto test = [&registry](auto func) {
  438. timer timer;
  439. registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>).each(func);
  440. timer.elapsed();
  441. };
  442. test([](const auto &...) {});
  443. test([](auto &... comp) {
  444. ((comp.x = {}), ...);
  445. });
  446. }
  447. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  448. entt::registry<> registry;
  449. std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
  450. for(std::uint64_t i = 0; i < 1000000L; i++) {
  451. const auto entity = registry.create();
  452. registry.assign<position>(entity);
  453. registry.assign<velocity>(entity);
  454. registry.assign<comp<0>>(entity);
  455. registry.assign<comp<1>>(entity);
  456. registry.assign<comp<2>>(entity);
  457. }
  458. auto test = [&registry](auto func) {
  459. using component_type = typename entt::registry<>::component_type;
  460. component_type types[] = {
  461. registry.type<position>(),
  462. registry.type<velocity>(),
  463. registry.type<comp<0>>(),
  464. registry.type<comp<1>>(),
  465. registry.type<comp<2>>()
  466. };
  467. timer timer;
  468. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  469. timer.elapsed();
  470. };
  471. test([](auto) {});
  472. test([&registry](auto entity) {
  473. registry.get<position>(entity).x = {};
  474. registry.get<velocity>(entity).x = {};
  475. registry.get<comp<0>>(entity).x = {};
  476. registry.get<comp<1>>(entity).x = {};
  477. registry.get<comp<2>>(entity).x = {};
  478. });
  479. }
  480. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  481. entt::registry<> registry;
  482. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  483. for(std::uint64_t i = 0; i < 1000000L; i++) {
  484. const auto entity = registry.create();
  485. registry.assign<velocity>(entity);
  486. registry.assign<comp<0>>(entity);
  487. registry.assign<comp<1>>(entity);
  488. registry.assign<comp<2>>(entity);
  489. if(i % 2) {
  490. registry.assign<position>(entity);
  491. }
  492. }
  493. auto test = [&registry](auto func) {
  494. using component_type = typename entt::registry<>::component_type;
  495. component_type types[] = {
  496. registry.type<position>(),
  497. registry.type<velocity>(),
  498. registry.type<comp<0>>(),
  499. registry.type<comp<1>>(),
  500. registry.type<comp<2>>()
  501. };
  502. timer timer;
  503. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  504. timer.elapsed();
  505. };
  506. test([](auto) {});
  507. test([&registry](auto entity) {
  508. registry.get<position>(entity).x = {};
  509. registry.get<velocity>(entity).x = {};
  510. registry.get<comp<0>>(entity).x = {};
  511. registry.get<comp<1>>(entity).x = {};
  512. registry.get<comp<2>>(entity).x = {};
  513. });
  514. }
  515. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  516. entt::registry<> registry;
  517. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  518. for(std::uint64_t i = 0; i < 1000000L; i++) {
  519. const auto entity = registry.create();
  520. registry.assign<velocity>(entity);
  521. registry.assign<comp<0>>(entity);
  522. registry.assign<comp<1>>(entity);
  523. registry.assign<comp<2>>(entity);
  524. if(i == 5000000L) {
  525. registry.assign<position>(entity);
  526. }
  527. }
  528. auto test = [&registry](auto func) {
  529. using component_type = typename entt::registry<>::component_type;
  530. component_type types[] = {
  531. registry.type<position>(),
  532. registry.type<velocity>(),
  533. registry.type<comp<0>>(),
  534. registry.type<comp<1>>(),
  535. registry.type<comp<2>>()
  536. };
  537. timer timer;
  538. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  539. timer.elapsed();
  540. };
  541. test([](auto) {});
  542. test([&registry](auto entity) {
  543. registry.get<position>(entity).x = {};
  544. registry.get<velocity>(entity).x = {};
  545. registry.get<comp<0>>(entity).x = {};
  546. registry.get<comp<1>>(entity).x = {};
  547. registry.get<comp<2>>(entity).x = {};
  548. });
  549. }
  550. TEST(Benchmark, IteratePathological) {
  551. entt::registry<> registry;
  552. std::cout << "Pathological case" << std::endl;
  553. for(std::uint64_t i = 0; i < 500000L; i++) {
  554. const auto entity = registry.create();
  555. registry.assign<position>(entity);
  556. registry.assign<velocity>(entity);
  557. registry.assign<comp<0>>(entity);
  558. }
  559. for(auto i = 0; i < 10; ++i) {
  560. registry.each([i = 0, &registry](const auto entity) mutable {
  561. if(i % 7) { registry.remove<position>(entity); }
  562. if(i % 11) { registry.remove<velocity>(entity); }
  563. if(i % 13) { registry.remove<comp<0>>(entity); }
  564. if(i % 17) { registry.destroy(entity); }
  565. });
  566. for(std::uint64_t i = 0; i < 50000L; i++) {
  567. const auto entity = registry.create();
  568. registry.assign<position>(entity);
  569. registry.assign<velocity>(entity);
  570. registry.assign<comp<0>>(entity);
  571. }
  572. }
  573. auto test = [&registry](auto func) {
  574. timer timer;
  575. registry.view<position, velocity, comp<0>>().each(func);
  576. timer.elapsed();
  577. };
  578. test([](const auto &...) {});
  579. test([](auto &... comp) {
  580. ((comp.x = {}), ...);
  581. });
  582. }
  583. TEST(Benchmark, IteratePathologicalNonOwningGroup) {
  584. entt::registry<> registry;
  585. registry.group<>(entt::get<position, velocity, comp<0>>);
  586. std::cout << "Pathological case" << std::endl;
  587. for(std::uint64_t i = 0; i < 500000L; i++) {
  588. const auto entity = registry.create();
  589. registry.assign<position>(entity);
  590. registry.assign<velocity>(entity);
  591. registry.assign<comp<0>>(entity);
  592. }
  593. for(auto i = 0; i < 10; ++i) {
  594. registry.each([i = 0, &registry](const auto entity) mutable {
  595. if(i % 7) { registry.remove<position>(entity); }
  596. if(i % 11) { registry.remove<velocity>(entity); }
  597. if(i % 13) { registry.remove<comp<0>>(entity); }
  598. if(i % 17) { registry.destroy(entity); }
  599. });
  600. for(std::uint64_t i = 0; i < 50000L; i++) {
  601. const auto entity = registry.create();
  602. registry.assign<position>(entity);
  603. registry.assign<velocity>(entity);
  604. registry.assign<comp<0>>(entity);
  605. }
  606. }
  607. auto test = [&registry](auto func) {
  608. timer timer;
  609. registry.group<>(entt::get<position, velocity, comp<0>>).each(func);
  610. timer.elapsed();
  611. };
  612. test([](const auto &...) {});
  613. test([](auto &... comp) {
  614. ((comp.x = {}), ...);
  615. });
  616. }
  617. TEST(Benchmark, IteratePathologicalFullOwningGroup) {
  618. entt::registry<> registry;
  619. registry.group<position, velocity, comp<0>>();
  620. std::cout << "Pathological case" << std::endl;
  621. for(std::uint64_t i = 0; i < 500000L; i++) {
  622. const auto entity = registry.create();
  623. registry.assign<position>(entity);
  624. registry.assign<velocity>(entity);
  625. registry.assign<comp<0>>(entity);
  626. }
  627. for(auto i = 0; i < 10; ++i) {
  628. registry.each([i = 0, &registry](const auto entity) mutable {
  629. if(i % 7) { registry.remove<position>(entity); }
  630. if(i % 11) { registry.remove<velocity>(entity); }
  631. if(i % 13) { registry.remove<comp<0>>(entity); }
  632. if(i % 17) { registry.destroy(entity); }
  633. });
  634. for(std::uint64_t i = 0; i < 50000L; i++) {
  635. const auto entity = registry.create();
  636. registry.assign<position>(entity);
  637. registry.assign<velocity>(entity);
  638. registry.assign<comp<0>>(entity);
  639. }
  640. }
  641. auto test = [&registry](auto func) {
  642. timer timer;
  643. registry.group<position, velocity, comp<0>>().each(func);
  644. timer.elapsed();
  645. };
  646. test([](const auto &...) {});
  647. test([](auto &... comp) {
  648. ((comp.x = {}), ...);
  649. });
  650. }
  651. TEST(Benchmark, IteratePathologicalPartialOwningGroup) {
  652. entt::registry<> registry;
  653. registry.group<position, velocity>(entt::get<comp<0>>);
  654. std::cout << "Pathological case" << std::endl;
  655. for(std::uint64_t i = 0; i < 500000L; i++) {
  656. const auto entity = registry.create();
  657. registry.assign<position>(entity);
  658. registry.assign<velocity>(entity);
  659. registry.assign<comp<0>>(entity);
  660. }
  661. for(auto i = 0; i < 10; ++i) {
  662. registry.each([i = 0, &registry](const auto entity) mutable {
  663. if(i % 7) { registry.remove<position>(entity); }
  664. if(i % 11) { registry.remove<velocity>(entity); }
  665. if(i % 13) { registry.remove<comp<0>>(entity); }
  666. if(i % 17) { registry.destroy(entity); }
  667. });
  668. for(std::uint64_t i = 0; i < 50000L; i++) {
  669. const auto entity = registry.create();
  670. registry.assign<position>(entity);
  671. registry.assign<velocity>(entity);
  672. registry.assign<comp<0>>(entity);
  673. }
  674. }
  675. auto test = [&registry](auto func) {
  676. timer timer;
  677. registry.group<position, velocity>(entt::get<comp<0>>).each(func);
  678. timer.elapsed();
  679. };
  680. test([](const auto &...) {});
  681. test([](auto &... comp) {
  682. ((comp.x = {}), ...);
  683. });
  684. }
  685. TEST(Benchmark, SortSingle) {
  686. entt::registry<> registry;
  687. std::cout << "Sort 150000 entities, one component" << std::endl;
  688. for(std::uint64_t i = 0; i < 150000L; i++) {
  689. const auto entity = registry.create();
  690. registry.assign<position>(entity, i, i);
  691. }
  692. timer timer;
  693. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  694. return lhs.x < rhs.x && lhs.y < rhs.y;
  695. });
  696. timer.elapsed();
  697. }
  698. TEST(Benchmark, SortMulti) {
  699. entt::registry<> registry;
  700. std::cout << "Sort 150000 entities, two components" << std::endl;
  701. for(std::uint64_t i = 0; i < 150000L; i++) {
  702. const auto entity = registry.create();
  703. registry.assign<position>(entity, i, i);
  704. registry.assign<velocity>(entity, i, i);
  705. }
  706. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  707. return lhs.x < rhs.x && lhs.y < rhs.y;
  708. });
  709. timer timer;
  710. registry.sort<velocity, position>();
  711. timer.elapsed();
  712. }
  713. TEST(Benchmark, AlmostSortedStdSort) {
  714. entt::registry<> registry;
  715. entt::registry<>::entity_type entities[3];
  716. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  717. for(std::uint64_t i = 0; i < 150000L; i++) {
  718. const auto entity = registry.create();
  719. registry.assign<position>(entity, i, i);
  720. if(!(i % 50000)) {
  721. entities[i / 50000] = entity;
  722. }
  723. }
  724. for(std::uint64_t i = 0; i < 3; ++i) {
  725. registry.destroy(entities[i]);
  726. const auto entity = registry.create();
  727. registry.assign<position>(entity, 50000 * i, 50000 * i);
  728. }
  729. timer timer;
  730. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  731. return lhs.x > rhs.x && lhs.y > rhs.y;
  732. });
  733. timer.elapsed();
  734. }
  735. TEST(Benchmark, AlmostSortedInsertionSort) {
  736. entt::registry<> registry;
  737. entt::registry<>::entity_type entities[3];
  738. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
  739. for(std::uint64_t i = 0; i < 150000L; i++) {
  740. const auto entity = registry.create();
  741. registry.assign<position>(entity, i, i);
  742. if(!(i % 50000)) {
  743. entities[i / 50000] = entity;
  744. }
  745. }
  746. for(std::uint64_t i = 0; i < 3; ++i) {
  747. registry.destroy(entities[i]);
  748. const auto entity = registry.create();
  749. registry.assign<position>(entity, 50000 * i, 50000 * i);
  750. }
  751. timer timer;
  752. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  753. return lhs.x > rhs.x && lhs.y > rhs.y;
  754. }, entt::insertion_sort{});
  755. timer.elapsed();
  756. }