Last active
September 9, 2022 03:03
-
-
Save bwrsandman/b94985d45e4846022492918b2a72076d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <execution> | |
#include <benchmark/benchmark.h> | |
#include <entt/entity/registry.hpp> | |
#include <glm/vec3.hpp> | |
static void BM_AoS(benchmark::State& state) | |
{ | |
struct Zoomer | |
{ | |
float value; | |
float destination; | |
float t; | |
float duration; | |
}; | |
entt::registry registry; | |
Zoomer value = {0.0f, 1.0f * state.iterations(), 0.0f, 1.0f * state.iterations()}; | |
std::vector<entt::entity> entities(10000); | |
auto view = registry.view<Zoomer>(); | |
registry.create(entities.begin(), entities.end()); | |
registry.insert<Zoomer>(entities.begin(), entities.end(), value); | |
for (int i = 0; auto _ : state) | |
{ | |
for (auto [entity, zoomer] : view.each()) | |
{ | |
zoomer.t = 1.0f * i; | |
zoomer.value = zoomer.destination * (zoomer.t / zoomer.duration); | |
} | |
++i; | |
} | |
} | |
static void BM_AoSPar(benchmark::State& state) | |
{ | |
struct Zoomer | |
{ | |
float value; | |
float destination; | |
float t; | |
float duration; | |
}; | |
entt::registry registry; | |
Zoomer value = {0.0f, 1.0f * state.iterations(), 0.0f, 1.0f * state.iterations()}; | |
std::vector<entt::entity> entities(10000); | |
auto view = registry.view<Zoomer>(); | |
registry.create(entities.begin(), entities.end()); | |
registry.insert<Zoomer>(entities.begin(), entities.end(), value); | |
for (int i = 0; auto _ : state) | |
{ | |
std::for_each(std::execution::par_unseq, view.begin(), view.end(), [&view, i](auto entity) { | |
auto& zoomer = view.get<Zoomer>(entity); | |
zoomer.t = 1.0f * i; | |
zoomer.value = zoomer.destination * (zoomer.t / zoomer.duration); | |
}); | |
++i; | |
} | |
} | |
static void BM_AoS3(benchmark::State& state) | |
{ | |
struct Zoomer | |
{ | |
glm::vec3 value; | |
glm::vec3 destination; | |
glm::vec3 t; | |
glm::vec3 duration; | |
}; | |
entt::registry registry; | |
Zoomer value = {glm::vec3(0.0f, 0.0f, 0.0f), | |
glm::vec3(1.0f * state.iterations(), 1.0f * state.iterations(), 1.0f * state.iterations()), | |
glm::vec3(0.0f, 0.0f, 0.0f), | |
glm::vec3(1.0f * state.iterations(), 1.0f * state.iterations(), 1.0f * state.iterations())}; | |
std::vector<entt::entity> entities(10000 / 3); | |
auto view = registry.view<Zoomer>(); | |
registry.create(entities.begin(), entities.end()); | |
registry.insert<Zoomer>(entities.begin(), entities.end(), value); | |
for (int i = 0; auto _ : state) | |
{ | |
for (auto [entity, zoomer] : view.each()) | |
{ | |
zoomer.t = glm::vec3(1.0f * i, 1.0f * i, 1.0f * i); | |
zoomer.value = zoomer.destination * (zoomer.t / zoomer.duration); | |
} | |
++i; | |
} | |
} | |
static void BM_AoS3Par(benchmark::State& state) | |
{ | |
struct Zoomer | |
{ | |
glm::vec3 value; | |
glm::vec3 destination; | |
glm::vec3 t; | |
glm::vec3 duration; | |
}; | |
entt::registry registry; | |
Zoomer value = {glm::vec3(0.0f, 0.0f, 0.0f), | |
glm::vec3(1.0f * state.iterations(), 1.0f * state.iterations(), 1.0f * state.iterations()), | |
glm::vec3(0.0f, 0.0f, 0.0f), | |
glm::vec3(1.0f * state.iterations(), 1.0f * state.iterations(), 1.0f * state.iterations())}; | |
std::vector<entt::entity> entities(10000 / 3); | |
auto view = registry.view<Zoomer>(); | |
registry.create(entities.begin(), entities.end()); | |
registry.insert<Zoomer>(entities.begin(), entities.end(), value); | |
for (int i = 0; auto _ : state) | |
{ | |
std::for_each(std::execution::par_unseq, view.begin(), view.end(), [&view, i](auto entity) { | |
auto& zoomer = view.get<Zoomer>(entity); | |
zoomer.t = glm::vec3(1.0f * i, 1.0f * i, 1.0f * i); | |
zoomer.value = zoomer.destination * (zoomer.t / zoomer.duration); | |
}); | |
++i; | |
} | |
} | |
static void BM_PerField(benchmark::State& state) | |
{ | |
struct ZoomerValue | |
{ | |
float value; | |
}; | |
struct ZoomerDestination | |
{ | |
float value; | |
}; | |
struct ZoomerT | |
{ | |
float value; | |
}; | |
struct ZoomerDuration | |
{ | |
float value; | |
}; | |
entt::registry registry; | |
ZoomerValue valueValue = {0.0f}; | |
ZoomerDestination valueDestination = {1.0f * state.iterations()}; | |
ZoomerT valueT = {0.0f}; | |
ZoomerDuration valueDuration = {1.0f * state.iterations()}; | |
std::vector<entt::entity> entities(10000); | |
auto view = registry.view<ZoomerValue, const ZoomerDestination, ZoomerT, const ZoomerDuration>(); | |
registry.create(entities.begin(), entities.end()); | |
registry.insert<ZoomerValue>(entities.begin(), entities.end(), valueValue); | |
registry.insert<ZoomerDestination>(entities.begin(), entities.end(), valueDestination); | |
registry.insert<ZoomerT>(entities.begin(), entities.end(), valueT); | |
registry.insert<ZoomerDuration>(entities.begin(), entities.end(), valueDuration); | |
for (int i = 0; auto _ : state) | |
{ | |
for (auto [entity, value, destination, t, duration] : view.each()) | |
{ | |
t.value = 1.0f * i; | |
value.value = destination.value * (t.value / duration.value); | |
} | |
++i; | |
} | |
} | |
static void BM_PerFieldPar(benchmark::State& state) | |
{ | |
struct ZoomerValue | |
{ | |
float value; | |
}; | |
struct ZoomerDestination | |
{ | |
float value; | |
}; | |
struct ZoomerT | |
{ | |
float value; | |
}; | |
struct ZoomerDuration | |
{ | |
float value; | |
}; | |
entt::registry registry; | |
ZoomerValue valueValue = {0.0f}; | |
ZoomerDestination valueDestination = {1.0f * state.iterations()}; | |
ZoomerT valueT = {0.0f}; | |
ZoomerDuration valueDuration = {1.0f * state.iterations()}; | |
std::vector<entt::entity> entities(10000); | |
auto view = registry.view<ZoomerValue, const ZoomerDestination, ZoomerT, const ZoomerDuration>(); | |
registry.create(entities.begin(), entities.end()); | |
registry.insert<ZoomerValue>(entities.begin(), entities.end(), valueValue); | |
registry.insert<ZoomerDestination>(entities.begin(), entities.end(), valueDestination); | |
registry.insert<ZoomerT>(entities.begin(), entities.end(), valueT); | |
registry.insert<ZoomerDuration>(entities.begin(), entities.end(), valueDuration); | |
for (int i = 0; auto _ : state) | |
{ | |
std::for_each(std::execution::par_unseq, view.begin(), view.end(), [&view, i](auto entity) { | |
auto& value = view.get<ZoomerValue>(entity); | |
const auto& destination = view.get<const ZoomerDestination>(entity); | |
auto& t = view.get<ZoomerT>(entity); | |
const auto& duration = view.get<const ZoomerDuration>(entity); | |
t.value = 1.0f * i; | |
value.value = destination.value * (t.value / duration.value); | |
}); | |
++i; | |
} | |
} | |
BENCHMARK(BM_AoS); | |
BENCHMARK(BM_AoS3); | |
BENCHMARK(BM_PerField); | |
BENCHMARK(BM_AoSPar); | |
BENCHMARK(BM_AoS3Par); | |
BENCHMARK(BM_PerFieldPar); | |
BENCHMARK_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment