Spent the weekend implementing archetypal ECS over and over again
int ecs_bench(void) {
World world;
for (size_t i = 0; i < ENTITIES; ++i) {
world.spawn().insert(Velocity {1.f, 2.f}, Position {0.f, 0.f});
}
float final_x = 0.f;
float final_y = 0.f;
world.add_system([&](Query<const Velocity, Position>) {
for (auto [v, p] : query) {
p.x += v.x;
p.y += v.y;
final_x += p.x;
final_y += p.y;
}
});
for (size_t i = 0; i < ITERATIONS; ++i) {
world.progress();
}
printf("Final: (%f, %f)\n", final_x, final_y);
}
int flecs_bench(void) {
flecs::world world;
for (size_t i = 0; i < ENTITIES; ++i) {
world.entity().set<Velocity>({1.0f, 2.0f}).set<Position>({0.0f, 0.0f});
}
float final_x = 0.f;
float final_y = 0.f;
world.system<const Velocity, Position>().each([&](const auto& v, auto& p) {
p.x += v.x;
p.y += v.y;
final_x += p.x;
final_y += p.y;
});
for (size_t i = 0; i < ITERATIONS; ++i) {
world.progress();
}
printf("Final: (%f, %f)\n", final_x, final_y);
}