| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- #include <gtest/gtest.h>
- #include <iostream>
- #include <cstddef>
- #include <cstdint>
- #include <chrono>
- #include <vector>
- #include <entt/entity/registry.hpp>
- struct Position {
- std::uint64_t x;
- std::uint64_t y;
- };
- struct Velocity {
- std::uint64_t x;
- std::uint64_t y;
- };
- template<std::size_t>
- struct Comp {};
- struct Timer final {
- Timer(): start{std::chrono::system_clock::now()} {}
- void elapsed() {
- auto now = std::chrono::system_clock::now();
- std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
- }
- private:
- std::chrono::time_point<std::chrono::system_clock> start;
- };
- TEST(Benchmark, Construct) {
- entt::DefaultRegistry registry;
- std::cout << "Constructing 1000000 entities" << std::endl;
- Timer timer;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create();
- }
- timer.elapsed();
- }
- TEST(Benchmark, Destroy) {
- entt::DefaultRegistry registry;
- std::vector<entt::DefaultRegistry::entity_type> entities{};
- std::cout << "Destroying 1000000 entities" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- entities.push_back(registry.create());
- }
- Timer timer;
- for(auto entity: entities) {
- registry.destroy(entity);
- }
- timer.elapsed();
- }
- TEST(Benchmark, IterateCreateDeleteSingleComponent) {
- entt::DefaultRegistry registry;
- std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
- Timer timer;
- auto view = registry.view<Position>();
- for(int i = 0; i < 10000; i++) {
- for(int j = 0; j < 10000; j++) {
- registry.create<Position>();
- }
- for(auto entity: view) {
- if(rand() % 2 == 0) {
- registry.destroy(entity);
- }
- }
- }
- timer.elapsed();
- }
- TEST(Benchmark, IterateSingleComponent1M) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, one component" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position>();
- }
- Timer timer;
- registry.view<Position>().each([](auto, auto &) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTwoComponents1M) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, two components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position, Velocity>();
- }
- Timer timer;
- registry.view<Position, Velocity>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTwoComponents1MHalf) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- auto entity = registry.create<Velocity>();
- if(i % 2) { registry.assign<Position>(entity); }
- }
- Timer timer;
- registry.view<Position, Velocity>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTwoComponents1MOne) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- auto entity = registry.create<Velocity>();
- if(i == 5000000L) { registry.assign<Position>(entity); }
- }
- Timer timer;
- registry.view<Position, Velocity>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTwoComponentsPersistent1M) {
- entt::DefaultRegistry registry;
- registry.prepare<Position, Velocity>();
- std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position, Velocity>();
- }
- Timer timer;
- registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateFiveComponents1M) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, five components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
- }
- Timer timer;
- registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateFiveComponents1MHalf) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
- if(i % 2) { registry.assign<Position>(entity); }
- }
- Timer timer;
- registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateFiveComponents1MOne) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
- if(i == 5000000L) { registry.assign<Position>(entity); }
- }
- Timer timer;
- registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateFiveComponentsPersistent1M) {
- entt::DefaultRegistry registry;
- registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
- std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
- }
- Timer timer;
- registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTenComponents1M) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- }
- Timer timer;
- registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTenComponents1MHalf) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- if(i % 2) { registry.assign<Position>(entity); }
- }
- Timer timer;
- registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTenComponents1MOne) {
- entt::DefaultRegistry registry;
- std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- if(i == 5000000L) { registry.assign<Position>(entity); }
- }
- Timer timer;
- registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, IterateTenComponentsPersistent1M) {
- entt::DefaultRegistry registry;
- registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
- }
- Timer timer;
- registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
- timer.elapsed();
- }
- TEST(Benchmark, SortSingle) {
- entt::DefaultRegistry registry;
- std::vector<entt::DefaultRegistry::entity_type> entities{};
- std::cout << "Sort 150000 entities, one component" << std::endl;
- for(std::uint64_t i = 0; i < 150000L; i++) {
- auto entity = registry.create<Position>({ i, i });
- entities.push_back(entity);
- }
- Timer timer;
- registry.sort<Position>([](const auto &lhs, const auto &rhs) {
- return lhs.x < rhs.x && lhs.y < rhs.y;
- });
- timer.elapsed();
- }
- TEST(Benchmark, SortMulti) {
- entt::DefaultRegistry registry;
- std::vector<entt::DefaultRegistry::entity_type> entities{};
- std::cout << "Sort 150000 entities, two components" << std::endl;
- for(std::uint64_t i = 0; i < 150000L; i++) {
- auto entity = registry.create<Position, Velocity>({ i, i }, { i, i });
- entities.push_back(entity);
- }
- registry.sort<Position>([](const auto &lhs, const auto &rhs) {
- return lhs.x < rhs.x && lhs.y < rhs.y;
- });
- Timer timer;
- registry.sort<Velocity, Position>();
- timer.elapsed();
- }
|