benchmark.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. #include <iostream>
  2. #include <cstddef>
  3. #include <cstdint>
  4. #include <chrono>
  5. #include <gtest/gtest.h>
  6. #include <entt/entity/registry.hpp>
  7. struct Position {
  8. std::uint64_t x;
  9. std::uint64_t y;
  10. };
  11. struct Velocity {
  12. std::uint64_t x;
  13. std::uint64_t y;
  14. };
  15. template<std::size_t>
  16. struct Comp { int x; };
  17. struct Timer final {
  18. Timer(): start{std::chrono::system_clock::now()} {}
  19. void elapsed() {
  20. auto now = std::chrono::system_clock::now();
  21. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  22. }
  23. private:
  24. std::chrono::time_point<std::chrono::system_clock> start;
  25. };
  26. TEST(Benchmark, Construct) {
  27. entt::DefaultRegistry registry;
  28. std::cout << "Constructing 1000000 entities" << std::endl;
  29. Timer timer;
  30. for(std::uint64_t i = 0; i < 1000000L; i++) {
  31. registry.create();
  32. }
  33. timer.elapsed();
  34. }
  35. TEST(Benchmark, Destroy) {
  36. entt::DefaultRegistry registry;
  37. std::cout << "Destroying 1000000 entities" << std::endl;
  38. for(std::uint64_t i = 0; i < 1000000L; i++) {
  39. registry.create();
  40. }
  41. Timer timer;
  42. registry.each([&registry](auto entity) {
  43. registry.destroy(entity);
  44. });
  45. timer.elapsed();
  46. }
  47. TEST(Benchmark, IterateCreateDeleteSingleComponent) {
  48. entt::DefaultRegistry registry;
  49. std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
  50. Timer timer;
  51. auto view = registry.view<Position>();
  52. for(int i = 0; i < 10000; i++) {
  53. for(int j = 0; j < 10000; j++) {
  54. const auto entity = registry.create();
  55. registry.assign<Position>(entity);
  56. }
  57. for(auto entity: view) {
  58. if(rand() % 2 == 0) {
  59. registry.destroy(entity);
  60. }
  61. }
  62. }
  63. timer.elapsed();
  64. }
  65. TEST(Benchmark, IterateSingleComponent1M) {
  66. entt::DefaultRegistry registry;
  67. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  68. for(std::uint64_t i = 0; i < 1000000L; i++) {
  69. const auto entity = registry.create();
  70. registry.assign<Position>(entity);
  71. }
  72. auto test = [&registry](auto func) {
  73. Timer timer;
  74. registry.view<Position>().each(func);
  75. timer.elapsed();
  76. };
  77. test([](auto, const auto &) {});
  78. test([](auto, auto &position) {
  79. position.x = {};
  80. });
  81. }
  82. TEST(Benchmark, IterateSingleComponentRaw1M) {
  83. entt::DefaultRegistry registry;
  84. std::cout << "Iterating over 1000000 entities, one component, raw view" << std::endl;
  85. for(std::uint64_t i = 0; i < 1000000L; i++) {
  86. const auto entity = registry.create();
  87. registry.assign<Position>(entity);
  88. }
  89. auto test = [&registry](auto func) {
  90. Timer timer;
  91. registry.view<Position>(entt::raw_t{}).each(func);
  92. timer.elapsed();
  93. };
  94. test([](const auto &) {});
  95. test([](auto &position) {
  96. position.x = {};
  97. });
  98. }
  99. TEST(Benchmark, IterateTwoComponents1M) {
  100. entt::DefaultRegistry registry;
  101. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  102. for(std::uint64_t i = 0; i < 1000000L; i++) {
  103. const auto entity = registry.create();
  104. registry.assign<Position>(entity);
  105. registry.assign<Velocity>(entity);
  106. }
  107. auto test = [&registry](auto func) {
  108. Timer timer;
  109. registry.view<Position, Velocity>().each(func);
  110. timer.elapsed();
  111. };
  112. test([](auto, const auto &...) {});
  113. test([](auto, auto &position, auto &velocity) {
  114. position.x = {};
  115. velocity.x = {};
  116. });
  117. }
  118. TEST(Benchmark, IterateTwoComponents1MHalf) {
  119. entt::DefaultRegistry registry;
  120. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  121. for(std::uint64_t i = 0; i < 1000000L; i++) {
  122. const auto entity = registry.create();
  123. registry.assign<Velocity>(entity);
  124. if(i % 2) {
  125. registry.assign<Position>(entity);
  126. }
  127. }
  128. auto test = [&registry](auto func) {
  129. Timer timer;
  130. registry.view<Position, Velocity>().each(func);
  131. timer.elapsed();
  132. };
  133. test([](auto, const auto &...) {});
  134. test([](auto, auto &position, auto &velocity) {
  135. position.x = {};
  136. velocity.x = {};
  137. });
  138. }
  139. TEST(Benchmark, IterateTwoComponents1MOne) {
  140. entt::DefaultRegistry registry;
  141. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  142. for(std::uint64_t i = 0; i < 1000000L; i++) {
  143. const auto entity = registry.create();
  144. registry.assign<Velocity>(entity);
  145. if(i == 5000000L) {
  146. registry.assign<Position>(entity);
  147. }
  148. }
  149. auto test = [&registry](auto func) {
  150. Timer timer;
  151. registry.view<Position, Velocity>().each(func);
  152. timer.elapsed();
  153. };
  154. test([](auto, const auto &...) {});
  155. test([](auto, auto &position, auto &velocity) {
  156. position.x = {};
  157. velocity.x = {};
  158. });
  159. }
  160. TEST(Benchmark, IterateTwoComponentsPersistent1M) {
  161. entt::DefaultRegistry registry;
  162. registry.prepare<Position, Velocity>();
  163. std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
  164. for(std::uint64_t i = 0; i < 1000000L; i++) {
  165. const auto entity = registry.create();
  166. registry.assign<Position>(entity);
  167. registry.assign<Velocity>(entity);
  168. }
  169. auto test = [&registry](auto func) {
  170. Timer timer;
  171. registry.view<Position, Velocity>(entt::persistent_t{}).each(func);
  172. timer.elapsed();
  173. };
  174. test([](auto, const auto &...) {});
  175. test([](auto, auto &position, auto &velocity) {
  176. position.x = {};
  177. velocity.x = {};
  178. });
  179. }
  180. TEST(Benchmark, IterateFiveComponents1M) {
  181. entt::DefaultRegistry registry;
  182. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  183. for(std::uint64_t i = 0; i < 1000000L; i++) {
  184. const auto entity = registry.create();
  185. registry.assign<Position>(entity);
  186. registry.assign<Velocity>(entity);
  187. registry.assign<Comp<1>>(entity);
  188. registry.assign<Comp<2>>(entity);
  189. registry.assign<Comp<3>>(entity);
  190. }
  191. auto test = [&registry](auto func) {
  192. Timer timer;
  193. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each(func);
  194. timer.elapsed();
  195. };
  196. test([](auto, const auto &...) {});
  197. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3) {
  198. position.x = {};
  199. velocity.x = {};
  200. comp1.x = {};
  201. comp2.x = {};
  202. comp3.x = {};
  203. });
  204. }
  205. TEST(Benchmark, IterateFiveComponents1MHalf) {
  206. entt::DefaultRegistry registry;
  207. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  208. for(std::uint64_t i = 0; i < 1000000L; i++) {
  209. const auto entity = registry.create();
  210. registry.assign<Velocity>(entity);
  211. registry.assign<Comp<1>>(entity);
  212. registry.assign<Comp<2>>(entity);
  213. registry.assign<Comp<3>>(entity);
  214. if(i % 2) {
  215. registry.assign<Position>(entity);
  216. }
  217. }
  218. auto test = [&registry](auto func) {
  219. Timer timer;
  220. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each(func);
  221. timer.elapsed();
  222. };
  223. test([](auto, const auto &...) {});
  224. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3) {
  225. position.x = {};
  226. velocity.x = {};
  227. comp1.x = {};
  228. comp2.x = {};
  229. comp3.x = {};
  230. });
  231. }
  232. TEST(Benchmark, IterateFiveComponents1MOne) {
  233. entt::DefaultRegistry registry;
  234. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  235. for(std::uint64_t i = 0; i < 1000000L; i++) {
  236. const auto entity = registry.create();
  237. registry.assign<Velocity>(entity);
  238. registry.assign<Comp<1>>(entity);
  239. registry.assign<Comp<2>>(entity);
  240. registry.assign<Comp<3>>(entity);
  241. if(i == 5000000L) {
  242. registry.assign<Position>(entity);
  243. }
  244. }
  245. auto test = [&registry](auto func) {
  246. Timer timer;
  247. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each(func);
  248. timer.elapsed();
  249. };
  250. test([](auto, const auto &...) {});
  251. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3) {
  252. position.x = {};
  253. velocity.x = {};
  254. comp1.x = {};
  255. comp2.x = {};
  256. comp3.x = {};
  257. });
  258. }
  259. TEST(Benchmark, IterateFiveComponentsPersistent1M) {
  260. entt::DefaultRegistry registry;
  261. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
  262. std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
  263. for(std::uint64_t i = 0; i < 1000000L; i++) {
  264. const auto entity = registry.create();
  265. registry.assign<Position>(entity);
  266. registry.assign<Velocity>(entity);
  267. registry.assign<Comp<1>>(entity);
  268. registry.assign<Comp<2>>(entity);
  269. registry.assign<Comp<3>>(entity);
  270. }
  271. auto test = [&registry](auto func) {
  272. Timer timer;
  273. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>(entt::persistent_t{}).each(func);
  274. timer.elapsed();
  275. };
  276. test([](auto, const auto &...) {});
  277. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3) {
  278. position.x = {};
  279. velocity.x = {};
  280. comp1.x = {};
  281. comp2.x = {};
  282. comp3.x = {};
  283. });
  284. }
  285. TEST(Benchmark, IterateTenComponents1M) {
  286. entt::DefaultRegistry registry;
  287. std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
  288. for(std::uint64_t i = 0; i < 1000000L; i++) {
  289. const auto entity = registry.create();
  290. registry.assign<Position>(entity);
  291. registry.assign<Velocity>(entity);
  292. registry.assign<Comp<1>>(entity);
  293. registry.assign<Comp<2>>(entity);
  294. registry.assign<Comp<3>>(entity);
  295. registry.assign<Comp<4>>(entity);
  296. registry.assign<Comp<5>>(entity);
  297. registry.assign<Comp<6>>(entity);
  298. registry.assign<Comp<7>>(entity);
  299. registry.assign<Comp<8>>(entity);
  300. }
  301. auto test = [&registry](auto func) {
  302. Timer timer;
  303. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each(func);
  304. timer.elapsed();
  305. };
  306. test([](auto, const auto &...) {});
  307. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3, auto &comp4, auto &comp5, auto &comp6, auto &comp7, auto &comp8) {
  308. position.x = {};
  309. velocity.x = {};
  310. comp1.x = {};
  311. comp2.x = {};
  312. comp3.x = {};
  313. comp4.x = {};
  314. comp5.x = {};
  315. comp6.x = {};
  316. comp7.x = {};
  317. comp8.x = {};
  318. });
  319. }
  320. TEST(Benchmark, IterateTenComponents1MHalf) {
  321. entt::DefaultRegistry registry;
  322. std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
  323. for(std::uint64_t i = 0; i < 1000000L; i++) {
  324. const auto entity = registry.create();
  325. registry.assign<Velocity>(entity);
  326. registry.assign<Comp<1>>(entity);
  327. registry.assign<Comp<2>>(entity);
  328. registry.assign<Comp<3>>(entity);
  329. registry.assign<Comp<4>>(entity);
  330. registry.assign<Comp<5>>(entity);
  331. registry.assign<Comp<6>>(entity);
  332. registry.assign<Comp<7>>(entity);
  333. registry.assign<Comp<8>>(entity);
  334. if(i % 2) {
  335. registry.assign<Position>(entity);
  336. }
  337. }
  338. auto test = [&registry](auto func) {
  339. Timer timer;
  340. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each(func);
  341. timer.elapsed();
  342. };
  343. test([](auto, auto &...) {});
  344. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3, auto &comp4, auto &comp5, auto &comp6, auto &comp7, auto &comp8) {
  345. position.x = {};
  346. velocity.x = {};
  347. comp1.x = {};
  348. comp2.x = {};
  349. comp3.x = {};
  350. comp4.x = {};
  351. comp5.x = {};
  352. comp6.x = {};
  353. comp7.x = {};
  354. comp8.x = {};
  355. });
  356. }
  357. TEST(Benchmark, IterateTenComponents1MOne) {
  358. entt::DefaultRegistry registry;
  359. std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
  360. for(std::uint64_t i = 0; i < 1000000L; i++) {
  361. const auto entity = registry.create();
  362. registry.assign<Velocity>(entity);
  363. registry.assign<Comp<1>>(entity);
  364. registry.assign<Comp<2>>(entity);
  365. registry.assign<Comp<3>>(entity);
  366. registry.assign<Comp<4>>(entity);
  367. registry.assign<Comp<5>>(entity);
  368. registry.assign<Comp<6>>(entity);
  369. registry.assign<Comp<7>>(entity);
  370. registry.assign<Comp<8>>(entity);
  371. if(i == 5000000L) {
  372. registry.assign<Position>(entity);
  373. }
  374. }
  375. auto test = [&registry](auto func) {
  376. Timer timer;
  377. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each(func);
  378. timer.elapsed();
  379. };
  380. test([](auto, const auto &...) {});
  381. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3, auto &comp4, auto &comp5, auto &comp6, auto &comp7, auto &comp8) {
  382. position.x = {};
  383. velocity.x = {};
  384. comp1.x = {};
  385. comp2.x = {};
  386. comp3.x = {};
  387. comp4.x = {};
  388. comp5.x = {};
  389. comp6.x = {};
  390. comp7.x = {};
  391. comp8.x = {};
  392. });
  393. }
  394. TEST(Benchmark, IterateTenComponentsPersistent1M) {
  395. entt::DefaultRegistry registry;
  396. registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
  397. std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
  398. for(std::uint64_t i = 0; i < 1000000L; i++) {
  399. const auto entity = registry.create();
  400. registry.assign<Position>(entity);
  401. registry.assign<Velocity>(entity);
  402. registry.assign<Comp<1>>(entity);
  403. registry.assign<Comp<2>>(entity);
  404. registry.assign<Comp<3>>(entity);
  405. registry.assign<Comp<4>>(entity);
  406. registry.assign<Comp<5>>(entity);
  407. registry.assign<Comp<6>>(entity);
  408. registry.assign<Comp<7>>(entity);
  409. registry.assign<Comp<8>>(entity);
  410. }
  411. auto test = [&registry](auto func) {
  412. Timer timer;
  413. registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>(entt::persistent_t{}).each(func);
  414. timer.elapsed();
  415. };
  416. test([](auto, const auto &...) {});
  417. test([](auto, auto &position, auto &velocity, auto &comp1, auto &comp2, auto &comp3, auto &comp4, auto &comp5, auto &comp6, auto &comp7, auto &comp8) {
  418. position.x = {};
  419. velocity.x = {};
  420. comp1.x = {};
  421. comp2.x = {};
  422. comp3.x = {};
  423. comp4.x = {};
  424. comp5.x = {};
  425. comp6.x = {};
  426. comp7.x = {};
  427. comp8.x = {};
  428. });
  429. }
  430. TEST(Benchmark, SortSingle) {
  431. entt::DefaultRegistry registry;
  432. std::cout << "Sort 150000 entities, one component" << std::endl;
  433. for(std::uint64_t i = 0; i < 150000L; i++) {
  434. const auto entity = registry.create();
  435. registry.assign<Position>(entity, i, i);
  436. }
  437. Timer timer;
  438. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  439. return lhs.x < rhs.x && lhs.y < rhs.y;
  440. });
  441. timer.elapsed();
  442. }
  443. TEST(Benchmark, SortMulti) {
  444. entt::DefaultRegistry registry;
  445. std::cout << "Sort 150000 entities, two components" << std::endl;
  446. for(std::uint64_t i = 0; i < 150000L; i++) {
  447. const auto entity = registry.create();
  448. registry.assign<Position>(entity, i, i);
  449. registry.assign<Velocity>(entity, i, i);
  450. }
  451. registry.sort<Position>([](const auto &lhs, const auto &rhs) {
  452. return lhs.x < rhs.x && lhs.y < rhs.y;
  453. });
  454. Timer timer;
  455. registry.sort<Velocity, Position>();
  456. timer.elapsed();
  457. }