benchmark.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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, IterateThreeComponents1M) {
  293. entt::registry<> registry;
  294. std::cout << "Iterating over 1000000 entities, three 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. }
  301. auto test = [&registry](auto func) {
  302. timer timer;
  303. registry.view<position, velocity, comp<0>>().each(func);
  304. timer.elapsed();
  305. };
  306. test([](const auto &...) {});
  307. test([](auto &... comp) {
  308. ((comp.x = {}), ...);
  309. });
  310. }
  311. TEST(Benchmark, IterateThreeComponents1MHalf) {
  312. entt::registry<> registry;
  313. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components" << std::endl;
  314. for(std::uint64_t i = 0; i < 1000000L; i++) {
  315. const auto entity = registry.create();
  316. registry.assign<velocity>(entity);
  317. registry.assign<comp<0>>(entity);
  318. if(i % 2) {
  319. registry.assign<position>(entity);
  320. }
  321. }
  322. auto test = [&registry](auto func) {
  323. timer timer;
  324. registry.view<position, velocity, comp<0>>().each(func);
  325. timer.elapsed();
  326. };
  327. test([](const auto &...) {});
  328. test([](auto &... comp) {
  329. ((comp.x = {}), ...);
  330. });
  331. }
  332. TEST(Benchmark, IterateThreeComponents1MOne) {
  333. entt::registry<> registry;
  334. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components" << std::endl;
  335. for(std::uint64_t i = 0; i < 1000000L; i++) {
  336. const auto entity = registry.create();
  337. registry.assign<velocity>(entity);
  338. registry.assign<comp<0>>(entity);
  339. if(i == 5000000L) {
  340. registry.assign<position>(entity);
  341. }
  342. }
  343. auto test = [&registry](auto func) {
  344. timer timer;
  345. registry.view<position, velocity, comp<0>>().each(func);
  346. timer.elapsed();
  347. };
  348. test([](const auto &...) {});
  349. test([](auto &... comp) {
  350. ((comp.x = {}), ...);
  351. });
  352. }
  353. TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) {
  354. entt::registry<> registry;
  355. registry.group<>(entt::get<position, velocity>);
  356. std::cout << "Iterating over 1000000 entities, three components, non owning group" << std::endl;
  357. for(std::uint64_t i = 0; i < 1000000L; i++) {
  358. const auto entity = registry.create();
  359. registry.assign<position>(entity);
  360. registry.assign<velocity>(entity);
  361. registry.assign<comp<0>>(entity);
  362. }
  363. auto test = [&registry](auto func) {
  364. timer timer;
  365. registry.group<>(entt::get<position, velocity, comp<0>>).each(func);
  366. timer.elapsed();
  367. };
  368. test([](const auto &...) {});
  369. test([](auto &... comp) {
  370. ((comp.x = {}), ...);
  371. });
  372. }
  373. TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) {
  374. entt::registry<> registry;
  375. registry.group<position, velocity, comp<0>>();
  376. std::cout << "Iterating over 1000000 entities, three components, full owning group" << std::endl;
  377. for(std::uint64_t i = 0; i < 1000000L; i++) {
  378. const auto entity = registry.create();
  379. registry.assign<position>(entity);
  380. registry.assign<velocity>(entity);
  381. registry.assign<comp<0>>(entity);
  382. }
  383. auto test = [&registry](auto func) {
  384. timer timer;
  385. registry.group<position, velocity, comp<0>>().each(func);
  386. timer.elapsed();
  387. };
  388. test([](const auto &...) {});
  389. test([](auto &... comp) {
  390. ((comp.x = {}), ...);
  391. });
  392. }
  393. TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) {
  394. entt::registry<> registry;
  395. registry.group<position, velocity>(entt::get<comp<0>>);
  396. std::cout << "Iterating over 1000000 entities, three components, partial owning group" << std::endl;
  397. for(std::uint64_t i = 0; i < 1000000L; i++) {
  398. const auto entity = registry.create();
  399. registry.assign<position>(entity);
  400. registry.assign<velocity>(entity);
  401. registry.assign<comp<0>>(entity);
  402. }
  403. auto test = [&registry](auto func) {
  404. timer timer;
  405. registry.group<position, velocity>(entt::get<comp<0>>).each(func);
  406. timer.elapsed();
  407. };
  408. test([](const auto &...) {});
  409. test([](auto &... comp) {
  410. ((comp.x = {}), ...);
  411. });
  412. }
  413. TEST(Benchmark, IterateThreeComponentsRuntime1M) {
  414. entt::registry<> registry;
  415. std::cout << "Iterating over 1000000 entities, three components, runtime view" << std::endl;
  416. for(std::uint64_t i = 0; i < 1000000L; i++) {
  417. const auto entity = registry.create();
  418. registry.assign<position>(entity);
  419. registry.assign<velocity>(entity);
  420. registry.assign<comp<0>>(entity);
  421. }
  422. auto test = [&registry](auto func) {
  423. using component_type = typename entt::registry<>::component_type;
  424. component_type types[] = { registry.type<position>(), registry.type<velocity>(), registry.type<comp<0>>() };
  425. timer timer;
  426. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  427. timer.elapsed();
  428. };
  429. test([](auto) {});
  430. test([&registry](auto entity) {
  431. registry.get<position>(entity).x = {};
  432. registry.get<velocity>(entity).x = {};
  433. registry.get<comp<0>>(entity).x = {};
  434. });
  435. }
  436. TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) {
  437. entt::registry<> registry;
  438. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components, runtime view" << std::endl;
  439. for(std::uint64_t i = 0; i < 1000000L; i++) {
  440. const auto entity = registry.create();
  441. registry.assign<velocity>(entity);
  442. registry.assign<comp<0>>(entity);
  443. if(i % 2) {
  444. registry.assign<position>(entity);
  445. }
  446. }
  447. auto test = [&registry](auto func) {
  448. using component_type = typename entt::registry<>::component_type;
  449. component_type types[] = { registry.type<position>(), registry.type<velocity>(), registry.type<comp<0>>() };
  450. timer timer;
  451. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  452. timer.elapsed();
  453. };
  454. test([](auto) {});
  455. test([&registry](auto entity) {
  456. registry.get<position>(entity).x = {};
  457. registry.get<velocity>(entity).x = {};
  458. registry.get<comp<0>>(entity).x = {};
  459. });
  460. }
  461. TEST(Benchmark, IterateThreeComponentsRuntime1MOne) {
  462. entt::registry<> registry;
  463. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components, runtime view" << std::endl;
  464. for(std::uint64_t i = 0; i < 1000000L; i++) {
  465. const auto entity = registry.create();
  466. registry.assign<velocity>(entity);
  467. registry.assign<comp<0>>(entity);
  468. if(i == 5000000L) {
  469. registry.assign<position>(entity);
  470. }
  471. }
  472. auto test = [&registry](auto func) {
  473. using component_type = typename entt::registry<>::component_type;
  474. component_type types[] = { registry.type<position>(), registry.type<velocity>(), registry.type<comp<0>>() };
  475. timer timer;
  476. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  477. timer.elapsed();
  478. };
  479. test([](auto) {});
  480. test([&registry](auto entity) {
  481. registry.get<position>(entity).x = {};
  482. registry.get<velocity>(entity).x = {};
  483. registry.get<comp<0>>(entity).x = {};
  484. });
  485. }
  486. TEST(Benchmark, IterateFiveComponents1M) {
  487. entt::registry<> registry;
  488. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  489. for(std::uint64_t i = 0; i < 1000000L; i++) {
  490. const auto entity = registry.create();
  491. registry.assign<position>(entity);
  492. registry.assign<velocity>(entity);
  493. registry.assign<comp<0>>(entity);
  494. registry.assign<comp<1>>(entity);
  495. registry.assign<comp<2>>(entity);
  496. }
  497. auto test = [&registry](auto func) {
  498. timer timer;
  499. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  500. timer.elapsed();
  501. };
  502. test([](const auto &...) {});
  503. test([](auto &... comp) {
  504. ((comp.x = {}), ...);
  505. });
  506. }
  507. TEST(Benchmark, IterateFiveComponents1MHalf) {
  508. entt::registry<> registry;
  509. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  510. for(std::uint64_t i = 0; i < 1000000L; i++) {
  511. const auto entity = registry.create();
  512. registry.assign<velocity>(entity);
  513. registry.assign<comp<0>>(entity);
  514. registry.assign<comp<1>>(entity);
  515. registry.assign<comp<2>>(entity);
  516. if(i % 2) {
  517. registry.assign<position>(entity);
  518. }
  519. }
  520. auto test = [&registry](auto func) {
  521. timer timer;
  522. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  523. timer.elapsed();
  524. };
  525. test([](const auto &...) {});
  526. test([](auto &... comp) {
  527. ((comp.x = {}), ...);
  528. });
  529. }
  530. TEST(Benchmark, IterateFiveComponents1MOne) {
  531. entt::registry<> registry;
  532. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  533. for(std::uint64_t i = 0; i < 1000000L; i++) {
  534. const auto entity = registry.create();
  535. registry.assign<velocity>(entity);
  536. registry.assign<comp<0>>(entity);
  537. registry.assign<comp<1>>(entity);
  538. registry.assign<comp<2>>(entity);
  539. if(i == 5000000L) {
  540. registry.assign<position>(entity);
  541. }
  542. }
  543. auto test = [&registry](auto func) {
  544. timer timer;
  545. registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  546. timer.elapsed();
  547. };
  548. test([](const auto &...) {});
  549. test([](auto &... comp) {
  550. ((comp.x = {}), ...);
  551. });
  552. }
  553. TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) {
  554. entt::registry<> registry;
  555. registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>);
  556. std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl;
  557. for(std::uint64_t i = 0; i < 1000000L; i++) {
  558. const auto entity = registry.create();
  559. registry.assign<position>(entity);
  560. registry.assign<velocity>(entity);
  561. registry.assign<comp<0>>(entity);
  562. registry.assign<comp<1>>(entity);
  563. registry.assign<comp<2>>(entity);
  564. }
  565. auto test = [&registry](auto func) {
  566. timer timer;
  567. registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>).each(func);
  568. timer.elapsed();
  569. };
  570. test([](const auto &...) {});
  571. test([](auto &... comp) {
  572. ((comp.x = {}), ...);
  573. });
  574. }
  575. TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) {
  576. entt::registry<> registry;
  577. registry.group<position, velocity, comp<0>, comp<1>, comp<2>>();
  578. std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl;
  579. for(std::uint64_t i = 0; i < 1000000L; i++) {
  580. const auto entity = registry.create();
  581. registry.assign<position>(entity);
  582. registry.assign<velocity>(entity);
  583. registry.assign<comp<0>>(entity);
  584. registry.assign<comp<1>>(entity);
  585. registry.assign<comp<2>>(entity);
  586. }
  587. auto test = [&registry](auto func) {
  588. timer timer;
  589. registry.group<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
  590. timer.elapsed();
  591. };
  592. test([](const auto &...) {});
  593. test([](auto &... comp) {
  594. ((comp.x = {}), ...);
  595. });
  596. }
  597. TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) {
  598. entt::registry<> registry;
  599. registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>);
  600. std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl;
  601. for(std::uint64_t i = 0; i < 1000000L; i++) {
  602. const auto entity = registry.create();
  603. registry.assign<position>(entity);
  604. registry.assign<velocity>(entity);
  605. registry.assign<comp<0>>(entity);
  606. registry.assign<comp<1>>(entity);
  607. registry.assign<comp<2>>(entity);
  608. }
  609. auto test = [&registry](auto func) {
  610. timer timer;
  611. registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>).each(func);
  612. timer.elapsed();
  613. };
  614. test([](const auto &...) {});
  615. test([](auto &... comp) {
  616. ((comp.x = {}), ...);
  617. });
  618. }
  619. TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) {
  620. entt::registry<> registry;
  621. registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>);
  622. std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) owning group" << std::endl;
  623. for(std::uint64_t i = 0; i < 1000000L; i++) {
  624. const auto entity = registry.create();
  625. registry.assign<position>(entity);
  626. registry.assign<velocity>(entity);
  627. registry.assign<comp<0>>(entity);
  628. registry.assign<comp<1>>(entity);
  629. registry.assign<comp<2>>(entity);
  630. }
  631. auto test = [&registry](auto func) {
  632. timer timer;
  633. registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>).each(func);
  634. timer.elapsed();
  635. };
  636. test([](const auto &...) {});
  637. test([](auto &... comp) {
  638. ((comp.x = {}), ...);
  639. });
  640. }
  641. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  642. entt::registry<> registry;
  643. std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
  644. for(std::uint64_t i = 0; i < 1000000L; i++) {
  645. const auto entity = registry.create();
  646. registry.assign<position>(entity);
  647. registry.assign<velocity>(entity);
  648. registry.assign<comp<0>>(entity);
  649. registry.assign<comp<1>>(entity);
  650. registry.assign<comp<2>>(entity);
  651. }
  652. auto test = [&registry](auto func) {
  653. using component_type = typename entt::registry<>::component_type;
  654. component_type types[] = {
  655. registry.type<position>(),
  656. registry.type<velocity>(),
  657. registry.type<comp<0>>(),
  658. registry.type<comp<1>>(),
  659. registry.type<comp<2>>()
  660. };
  661. timer timer;
  662. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  663. timer.elapsed();
  664. };
  665. test([](auto) {});
  666. test([&registry](auto entity) {
  667. registry.get<position>(entity).x = {};
  668. registry.get<velocity>(entity).x = {};
  669. registry.get<comp<0>>(entity).x = {};
  670. registry.get<comp<1>>(entity).x = {};
  671. registry.get<comp<2>>(entity).x = {};
  672. });
  673. }
  674. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  675. entt::registry<> registry;
  676. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  677. for(std::uint64_t i = 0; i < 1000000L; i++) {
  678. const auto entity = registry.create();
  679. registry.assign<velocity>(entity);
  680. registry.assign<comp<0>>(entity);
  681. registry.assign<comp<1>>(entity);
  682. registry.assign<comp<2>>(entity);
  683. if(i % 2) {
  684. registry.assign<position>(entity);
  685. }
  686. }
  687. auto test = [&registry](auto func) {
  688. using component_type = typename entt::registry<>::component_type;
  689. component_type types[] = {
  690. registry.type<position>(),
  691. registry.type<velocity>(),
  692. registry.type<comp<0>>(),
  693. registry.type<comp<1>>(),
  694. registry.type<comp<2>>()
  695. };
  696. timer timer;
  697. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  698. timer.elapsed();
  699. };
  700. test([](auto) {});
  701. test([&registry](auto entity) {
  702. registry.get<position>(entity).x = {};
  703. registry.get<velocity>(entity).x = {};
  704. registry.get<comp<0>>(entity).x = {};
  705. registry.get<comp<1>>(entity).x = {};
  706. registry.get<comp<2>>(entity).x = {};
  707. });
  708. }
  709. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  710. entt::registry<> registry;
  711. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  712. for(std::uint64_t i = 0; i < 1000000L; i++) {
  713. const auto entity = registry.create();
  714. registry.assign<velocity>(entity);
  715. registry.assign<comp<0>>(entity);
  716. registry.assign<comp<1>>(entity);
  717. registry.assign<comp<2>>(entity);
  718. if(i == 5000000L) {
  719. registry.assign<position>(entity);
  720. }
  721. }
  722. auto test = [&registry](auto func) {
  723. using component_type = typename entt::registry<>::component_type;
  724. component_type types[] = {
  725. registry.type<position>(),
  726. registry.type<velocity>(),
  727. registry.type<comp<0>>(),
  728. registry.type<comp<1>>(),
  729. registry.type<comp<2>>()
  730. };
  731. timer timer;
  732. registry.runtime_view(std::begin(types), std::end(types)).each(func);
  733. timer.elapsed();
  734. };
  735. test([](auto) {});
  736. test([&registry](auto entity) {
  737. registry.get<position>(entity).x = {};
  738. registry.get<velocity>(entity).x = {};
  739. registry.get<comp<0>>(entity).x = {};
  740. registry.get<comp<1>>(entity).x = {};
  741. registry.get<comp<2>>(entity).x = {};
  742. });
  743. }
  744. TEST(Benchmark, IteratePathological) {
  745. entt::registry<> registry;
  746. std::cout << "Pathological case" << std::endl;
  747. for(std::uint64_t i = 0; i < 500000L; i++) {
  748. const auto entity = registry.create();
  749. registry.assign<position>(entity);
  750. registry.assign<velocity>(entity);
  751. registry.assign<comp<0>>(entity);
  752. }
  753. for(auto i = 0; i < 10; ++i) {
  754. registry.each([i = 0, &registry](const auto entity) mutable {
  755. if(i % 7) { registry.remove<position>(entity); }
  756. if(i % 11) { registry.remove<velocity>(entity); }
  757. if(i % 13) { registry.remove<comp<0>>(entity); }
  758. if(i % 17) { registry.destroy(entity); }
  759. });
  760. for(std::uint64_t i = 0; i < 50000L; i++) {
  761. const auto entity = registry.create();
  762. registry.assign<position>(entity);
  763. registry.assign<velocity>(entity);
  764. registry.assign<comp<0>>(entity);
  765. }
  766. }
  767. auto test = [&registry](auto func) {
  768. timer timer;
  769. registry.view<position, velocity, comp<0>>().each(func);
  770. timer.elapsed();
  771. };
  772. test([](const auto &...) {});
  773. test([](auto &... comp) {
  774. ((comp.x = {}), ...);
  775. });
  776. }
  777. TEST(Benchmark, IteratePathologicalNonOwningGroup) {
  778. entt::registry<> registry;
  779. registry.group<>(entt::get<position, velocity, comp<0>>);
  780. std::cout << "Pathological case" << std::endl;
  781. for(std::uint64_t i = 0; i < 500000L; i++) {
  782. const auto entity = registry.create();
  783. registry.assign<position>(entity);
  784. registry.assign<velocity>(entity);
  785. registry.assign<comp<0>>(entity);
  786. }
  787. for(auto i = 0; i < 10; ++i) {
  788. registry.each([i = 0, &registry](const auto entity) mutable {
  789. if(i % 7) { registry.remove<position>(entity); }
  790. if(i % 11) { registry.remove<velocity>(entity); }
  791. if(i % 13) { registry.remove<comp<0>>(entity); }
  792. if(i % 17) { registry.destroy(entity); }
  793. });
  794. for(std::uint64_t i = 0; i < 50000L; i++) {
  795. const auto entity = registry.create();
  796. registry.assign<position>(entity);
  797. registry.assign<velocity>(entity);
  798. registry.assign<comp<0>>(entity);
  799. }
  800. }
  801. auto test = [&registry](auto func) {
  802. timer timer;
  803. registry.group<>(entt::get<position, velocity, comp<0>>).each(func);
  804. timer.elapsed();
  805. };
  806. test([](const auto &...) {});
  807. test([](auto &... comp) {
  808. ((comp.x = {}), ...);
  809. });
  810. }
  811. TEST(Benchmark, IteratePathologicalFullOwningGroup) {
  812. entt::registry<> registry;
  813. registry.group<position, velocity, comp<0>>();
  814. std::cout << "Pathological case" << std::endl;
  815. for(std::uint64_t i = 0; i < 500000L; i++) {
  816. const auto entity = registry.create();
  817. registry.assign<position>(entity);
  818. registry.assign<velocity>(entity);
  819. registry.assign<comp<0>>(entity);
  820. }
  821. for(auto i = 0; i < 10; ++i) {
  822. registry.each([i = 0, &registry](const auto entity) mutable {
  823. if(i % 7) { registry.remove<position>(entity); }
  824. if(i % 11) { registry.remove<velocity>(entity); }
  825. if(i % 13) { registry.remove<comp<0>>(entity); }
  826. if(i % 17) { registry.destroy(entity); }
  827. });
  828. for(std::uint64_t i = 0; i < 50000L; i++) {
  829. const auto entity = registry.create();
  830. registry.assign<position>(entity);
  831. registry.assign<velocity>(entity);
  832. registry.assign<comp<0>>(entity);
  833. }
  834. }
  835. auto test = [&registry](auto func) {
  836. timer timer;
  837. registry.group<position, velocity, comp<0>>().each(func);
  838. timer.elapsed();
  839. };
  840. test([](const auto &...) {});
  841. test([](auto &... comp) {
  842. ((comp.x = {}), ...);
  843. });
  844. }
  845. TEST(Benchmark, IteratePathologicalPartialOwningGroup) {
  846. entt::registry<> registry;
  847. registry.group<position, velocity>(entt::get<comp<0>>);
  848. std::cout << "Pathological case" << std::endl;
  849. for(std::uint64_t i = 0; i < 500000L; i++) {
  850. const auto entity = registry.create();
  851. registry.assign<position>(entity);
  852. registry.assign<velocity>(entity);
  853. registry.assign<comp<0>>(entity);
  854. }
  855. for(auto i = 0; i < 10; ++i) {
  856. registry.each([i = 0, &registry](const auto entity) mutable {
  857. if(i % 7) { registry.remove<position>(entity); }
  858. if(i % 11) { registry.remove<velocity>(entity); }
  859. if(i % 13) { registry.remove<comp<0>>(entity); }
  860. if(i % 17) { registry.destroy(entity); }
  861. });
  862. for(std::uint64_t i = 0; i < 50000L; i++) {
  863. const auto entity = registry.create();
  864. registry.assign<position>(entity);
  865. registry.assign<velocity>(entity);
  866. registry.assign<comp<0>>(entity);
  867. }
  868. }
  869. auto test = [&registry](auto func) {
  870. timer timer;
  871. registry.group<position, velocity>(entt::get<comp<0>>).each(func);
  872. timer.elapsed();
  873. };
  874. test([](const auto &...) {});
  875. test([](auto &... comp) {
  876. ((comp.x = {}), ...);
  877. });
  878. }
  879. TEST(Benchmark, SortSingle) {
  880. entt::registry<> registry;
  881. std::cout << "Sort 150000 entities, one component" << std::endl;
  882. for(std::uint64_t i = 0; i < 150000L; i++) {
  883. const auto entity = registry.create();
  884. registry.assign<position>(entity, i, i);
  885. }
  886. timer timer;
  887. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  888. return lhs.x < rhs.x && lhs.y < rhs.y;
  889. });
  890. timer.elapsed();
  891. }
  892. TEST(Benchmark, SortMulti) {
  893. entt::registry<> registry;
  894. std::cout << "Sort 150000 entities, two components" << std::endl;
  895. for(std::uint64_t i = 0; i < 150000L; i++) {
  896. const auto entity = registry.create();
  897. registry.assign<position>(entity, i, i);
  898. registry.assign<velocity>(entity, i, i);
  899. }
  900. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  901. return lhs.x < rhs.x && lhs.y < rhs.y;
  902. });
  903. timer timer;
  904. registry.sort<velocity, position>();
  905. timer.elapsed();
  906. }
  907. TEST(Benchmark, AlmostSortedStdSort) {
  908. entt::registry<> registry;
  909. entt::registry<>::entity_type entities[3];
  910. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  911. for(std::uint64_t i = 0; i < 150000L; i++) {
  912. const auto entity = registry.create();
  913. registry.assign<position>(entity, i, i);
  914. if(!(i % 50000)) {
  915. entities[i / 50000] = entity;
  916. }
  917. }
  918. for(std::uint64_t i = 0; i < 3; ++i) {
  919. registry.destroy(entities[i]);
  920. const auto entity = registry.create();
  921. registry.assign<position>(entity, 50000 * i, 50000 * i);
  922. }
  923. timer timer;
  924. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  925. return lhs.x > rhs.x && lhs.y > rhs.y;
  926. });
  927. timer.elapsed();
  928. }
  929. TEST(Benchmark, AlmostSortedInsertionSort) {
  930. entt::registry<> registry;
  931. entt::registry<>::entity_type entities[3];
  932. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
  933. for(std::uint64_t i = 0; i < 150000L; i++) {
  934. const auto entity = registry.create();
  935. registry.assign<position>(entity, i, i);
  936. if(!(i % 50000)) {
  937. entities[i / 50000] = entity;
  938. }
  939. }
  940. for(std::uint64_t i = 0; i < 3; ++i) {
  941. registry.destroy(entities[i]);
  942. const auto entity = registry.create();
  943. registry.assign<position>(entity, 50000 * i, 50000 * i);
  944. }
  945. timer timer;
  946. registry.sort<position>([](const auto &lhs, const auto &rhs) {
  947. return lhs.x > rhs.x && lhs.y > rhs.y;
  948. }, entt::insertion_sort{});
  949. timer.elapsed();
  950. }