| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235 |
- #include <iostream>
- #include <cstddef>
- #include <cstdint>
- #include <chrono>
- #include <iterator>
- #include <gtest/gtest.h>
- #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 { int x; };
- 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::registry 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, ConstructMany) {
- entt::registry registry;
- std::vector<entt::entity> entities(1000000);
- std::cout << "Constructing 1000000 entities at once" << std::endl;
- timer timer;
- registry.create(entities.begin(), entities.end());
- timer.elapsed();
- }
- TEST(Benchmark, ConstructManyAndAssignComponents) {
- entt::registry registry;
- std::vector<entt::entity> entities(1000000);
- std::cout << "Constructing 1000000 entities at once and assign components" << std::endl;
- timer timer;
- registry.create(entities.begin(), entities.end());
- for(const auto entity: entities) {
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- }
- timer.elapsed();
- }
- TEST(Benchmark, ConstructManyWithComponents) {
- entt::registry registry;
- std::vector<entt::entity> entities(1000000);
- std::cout << "Constructing 1000000 entities at once with components" << std::endl;
- timer timer;
- registry.create<position, velocity>(entities.begin(), entities.end());
- timer.elapsed();
- }
- TEST(Benchmark, Destroy) {
- entt::registry registry;
- std::cout << "Destroying 1000000 entities" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- registry.create();
- }
- timer timer;
- registry.each([®istry](auto entity) {
- registry.destroy(entity);
- });
- timer.elapsed();
- }
- TEST(Benchmark, IterateCreateDeleteSingleComponent) {
- entt::registry 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++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- }
- for(auto entity: view) {
- if(rand() % 2 == 0) {
- registry.destroy(entity);
- }
- }
- }
- timer.elapsed();
- }
- TEST(Benchmark, IterateSingleComponent1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, one component" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateSingleComponentRuntime1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, one component, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateTwoComponents1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, two components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateTwoComponents1MHalf) {
- entt::registry 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++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- if(i % 2) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateTwoComponents1MOne) {
- entt::registry 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++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- if(i == 5000000L) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) {
- entt::registry registry;
- registry.group<>(entt::get<position, velocity>);
- std::cout << "Iterating over 1000000 entities, two components, non owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<>(entt::get<position, velocity>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) {
- entt::registry registry;
- registry.group<position, velocity>();
- std::cout << "Iterating over 1000000 entities, two components, full owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) {
- entt::registry registry;
- registry.group<position>(entt::get<velocity>);
- std::cout << "Iterating over 1000000 entities, two components, partial owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position>(entt::get<velocity>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateTwoComponentsRuntime1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>(), registry.type<velocity>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- if(i % 2) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>(), registry.type<velocity>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- if(i == 5000000L) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>(), registry.type<velocity>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateThreeComponents1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, three components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateThreeComponents1MHalf) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- if(i % 2) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateThreeComponents1MOne) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- if(i == 5000000L) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) {
- entt::registry registry;
- registry.group<>(entt::get<position, velocity, comp<0>>);
- std::cout << "Iterating over 1000000 entities, three components, non owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<>(entt::get<position, velocity, comp<0>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) {
- entt::registry registry;
- registry.group<position, velocity, comp<0>>();
- std::cout << "Iterating over 1000000 entities, three components, full owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity, comp<0>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) {
- entt::registry registry;
- registry.group<position, velocity>(entt::get<comp<0>>);
- std::cout << "Iterating over 1000000 entities, three components, partial owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity>(entt::get<comp<0>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateThreeComponentsRuntime1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, three components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>(), registry.type<velocity>(), registry.type<comp<0>>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- registry.get<comp<0>>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- if(i % 2) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>(), registry.type<velocity>(), registry.type<comp<0>>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- registry.get<comp<0>>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateThreeComponentsRuntime1MOne) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- if(i == 5000000L) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = { registry.type<position>(), registry.type<velocity>(), registry.type<comp<0>>() };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- registry.get<comp<0>>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateFiveComponents1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, five components" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponents1MHalf) {
- entt::registry 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++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- if(i % 2) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponents1MOne) {
- entt::registry 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++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- if(i == 5000000L) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) {
- entt::registry registry;
- registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>);
- std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) {
- entt::registry registry;
- registry.group<position, velocity, comp<0>, comp<1>, comp<2>>();
- std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity, comp<0>, comp<1>, comp<2>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) {
- entt::registry registry;
- registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>);
- std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) {
- entt::registry registry;
- registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>);
- std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) owning group" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IterateFiveComponentsRuntime1M) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = {
- registry.type<position>(),
- registry.type<velocity>(),
- registry.type<comp<0>>(),
- registry.type<comp<1>>(),
- registry.type<comp<2>>()
- };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- registry.get<comp<0>>(entity).x = {};
- registry.get<comp<1>>(entity).x = {};
- registry.get<comp<2>>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- if(i % 2) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = {
- registry.type<position>(),
- registry.type<velocity>(),
- registry.type<comp<0>>(),
- registry.type<comp<1>>(),
- registry.type<comp<2>>()
- };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- registry.get<comp<0>>(entity).x = {};
- registry.get<comp<1>>(entity).x = {};
- registry.get<comp<2>>(entity).x = {};
- });
- }
- TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
- entt::registry registry;
- std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
- for(std::uint64_t i = 0; i < 1000000L; i++) {
- const auto entity = registry.create();
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- registry.assign<comp<1>>(entity);
- registry.assign<comp<2>>(entity);
- if(i == 5000000L) {
- registry.assign<position>(entity);
- }
- }
- auto test = [®istry](auto func) {
- using component_type = typename entt::registry::component_type;
- component_type types[] = {
- registry.type<position>(),
- registry.type<velocity>(),
- registry.type<comp<0>>(),
- registry.type<comp<1>>(),
- registry.type<comp<2>>()
- };
- timer timer;
- registry.runtime_view(std::begin(types), std::end(types)).each(func);
- timer.elapsed();
- };
- test([](auto) {});
- test([®istry](auto entity) {
- registry.get<position>(entity).x = {};
- registry.get<velocity>(entity).x = {};
- registry.get<comp<0>>(entity).x = {};
- registry.get<comp<1>>(entity).x = {};
- registry.get<comp<2>>(entity).x = {};
- });
- }
- TEST(Benchmark, IteratePathological) {
- entt::registry registry;
- std::cout << "Pathological case" << std::endl;
- for(std::uint64_t i = 0; i < 500000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- for(auto i = 0; i < 10; ++i) {
- registry.each([i = 0, ®istry](const auto entity) mutable {
- if(i % 7) { registry.remove<position>(entity); }
- if(i % 11) { registry.remove<velocity>(entity); }
- if(i % 13) { registry.remove<comp<0>>(entity); }
- if(i % 17) { registry.destroy(entity); }
- });
- for(std::uint64_t j = 0; j < 50000L; j++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.view<position, velocity, comp<0>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IteratePathologicalNonOwningGroup) {
- entt::registry registry;
- registry.group<>(entt::get<position, velocity, comp<0>>);
- std::cout << "Pathological case" << std::endl;
- for(std::uint64_t i = 0; i < 500000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- for(auto i = 0; i < 10; ++i) {
- registry.each([i = 0, ®istry](const auto entity) mutable {
- if(i % 7) { registry.remove<position>(entity); }
- if(i % 11) { registry.remove<velocity>(entity); }
- if(i % 13) { registry.remove<comp<0>>(entity); }
- if(i % 17) { registry.destroy(entity); }
- });
- for(std::uint64_t j = 0; j < 50000L; j++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<>(entt::get<position, velocity, comp<0>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IteratePathologicalFullOwningGroup) {
- entt::registry registry;
- registry.group<position, velocity, comp<0>>();
- std::cout << "Pathological case" << std::endl;
- for(std::uint64_t i = 0; i < 500000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- for(auto i = 0; i < 10; ++i) {
- registry.each([i = 0, ®istry](const auto entity) mutable {
- if(i % 7) { registry.remove<position>(entity); }
- if(i % 11) { registry.remove<velocity>(entity); }
- if(i % 13) { registry.remove<comp<0>>(entity); }
- if(i % 17) { registry.destroy(entity); }
- });
- for(std::uint64_t j = 0; j < 50000L; j++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity, comp<0>>().each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, IteratePathologicalPartialOwningGroup) {
- entt::registry registry;
- registry.group<position, velocity>(entt::get<comp<0>>);
- std::cout << "Pathological case" << std::endl;
- for(std::uint64_t i = 0; i < 500000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- for(auto i = 0; i < 10; ++i) {
- registry.each([i = 0, ®istry](const auto entity) mutable {
- if(i % 7) { registry.remove<position>(entity); }
- if(i % 11) { registry.remove<velocity>(entity); }
- if(i % 13) { registry.remove<comp<0>>(entity); }
- if(i % 17) { registry.destroy(entity); }
- });
- for(std::uint64_t j = 0; j < 50000L; j++) {
- const auto entity = registry.create();
- registry.assign<position>(entity);
- registry.assign<velocity>(entity);
- registry.assign<comp<0>>(entity);
- }
- }
- auto test = [®istry](auto func) {
- timer timer;
- registry.group<position, velocity>(entt::get<comp<0>>).each(func);
- timer.elapsed();
- };
- test([](const auto &...) {});
- test([](auto &... comp) {
- ((comp.x = {}), ...);
- });
- }
- TEST(Benchmark, SortSingle) {
- entt::registry registry;
- std::cout << "Sort 150000 entities, one component" << std::endl;
- for(std::uint64_t i = 0; i < 150000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity, i, i);
- }
- 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::registry registry;
- std::cout << "Sort 150000 entities, two components" << std::endl;
- for(std::uint64_t i = 0; i < 150000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity, i, i);
- registry.assign<velocity>(entity, i, i);
- }
- 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();
- }
- TEST(Benchmark, AlmostSortedStdSort) {
- entt::registry registry;
- entt::entity entities[3];
- std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
- for(std::uint64_t i = 0; i < 150000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity, i, i);
- if(!(i % 50000)) {
- entities[i / 50000] = entity;
- }
- }
- for(std::uint64_t i = 0; i < 3; ++i) {
- registry.destroy(entities[i]);
- const auto entity = registry.create();
- registry.assign<position>(entity, 50000 * i, 50000 * i);
- }
- timer timer;
- registry.sort<position>([](const auto &lhs, const auto &rhs) {
- return lhs.x > rhs.x && lhs.y > rhs.y;
- });
- timer.elapsed();
- }
- TEST(Benchmark, AlmostSortedInsertionSort) {
- entt::registry registry;
- entt::entity entities[3];
- std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
- for(std::uint64_t i = 0; i < 150000L; i++) {
- const auto entity = registry.create();
- registry.assign<position>(entity, i, i);
- if(!(i % 50000)) {
- entities[i / 50000] = entity;
- }
- }
- for(std::uint64_t i = 0; i < 3; ++i) {
- registry.destroy(entities[i]);
- const auto entity = registry.create();
- registry.assign<position>(entity, 50000 * i, 50000 * i);
- }
- timer timer;
- registry.sort<position>([](const auto &lhs, const auto &rhs) {
- return lhs.x > rhs.x && lhs.y > rhs.y;
- }, entt::insertion_sort{});
- timer.elapsed();
- }
|