Skip to content

Instantly share code, notes, and snippets.

@Hexlord
Last active April 2, 2022 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hexlord/0503428e4ed2fd73701bed1c5ae5bb89 to your computer and use it in GitHub Desktop.
Save Hexlord/0503428e4ed2fd73701bed1c5ae5bb89 to your computer and use it in GitHub Desktop.
#include "flecs.h"
#include "inttypes.h"
#include <cstdio>
#include <ctime>
#include <atomic>
constexpr int ENTITIES = 1000;
constexpr int MILLION = 1000000;
constexpr int SAMPLE_COUNT = 10000;
constexpr bool MILTI_THREADED = true;
std::atomic<uint32_t> Atomic;
int main() {
{
struct Comp1 {
int field1;
int field2;
};
struct Tag {};
flecs::world ECS;
ECS.use<Comp1>();
ECS.use<Tag>();
ECS.set_threads(8);
printf("\n");
ecs_time_t t = {0};
Atomic = 0;
ECS.system<Comp1>().multi_threaded(MILTI_THREADED).iter([&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
Entity.add<Tag>();
}
});
ECS.system().no_staging().iter([](flecs::iter&){});
ECS.system<Comp1, Tag>().multi_threaded(MILTI_THREADED).iter([&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
Atomic++;
Entity.remove<Tag>();
}
});
for (int i = 0; i < ENTITIES; ++i) {
ECS.entity().add<Comp1>();
}
ECS.progress();
ECS.progress();
{
double sum = 0, min = 10e10, max = 0;
for (int s = 0; s < SAMPLE_COUNT; s++) {
ecs_time_measure(&t);
ECS.progress();
double v = ecs_time_measure(&t);
min = v < min ? v : min;
max = v > max ? v : max;
sum += v;
}
sum /= (double)SAMPLE_COUNT;
printf("progress with tag: min: %.2fus, avg: %.2fus, max: %.2fus\n",
min * MILLION, sum * MILLION, max * MILLION);
printf("atomic value: %u\n", Atomic.load());
}
}
{
struct Comp1 {
int field1;
int field2;
bool Dirty;
};
flecs::world ECS;
ECS.use<Comp1>();
ECS.set_threads(8);
ecs_time_t t = {0};
Atomic = 0;
ECS.system<Comp1>().multi_threaded(MILTI_THREADED).iter(
[&](flecs::iter &Iter, Comp1 *Comp) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
Comp[Index].Dirty = true;
}
});
ECS.system().no_staging().iter([](flecs::iter&){});
ECS.system<Comp1>().multi_threaded(MILTI_THREADED).iter(
[&](flecs::iter &Iter, Comp1 *Comp) {
for (auto Index : Iter) {
if(Comp[Index].Dirty) {
auto Entity = Iter.entity(Index);
Atomic++;
Comp[Index].Dirty = false;
}
}
});
for (int i = 0; i < ENTITIES; ++i) {
ECS.entity().add<Comp1>();
}
ECS.progress();
ECS.progress();
{
double sum = 0, min = 10e10, max = 0;
for (int s = 0; s < SAMPLE_COUNT; s++) {
ecs_time_measure(&t);
ECS.progress();
double v = ecs_time_measure(&t);
min = v < min ? v : min;
max = v > max ? v : max;
sum += v;
}
sum /= (double)SAMPLE_COUNT;
printf("progress no tag: min: %.2fus, avg: %.2fus, max: %.2fus\n",
min * MILLION, sum * MILLION, max * MILLION);
printf("atomic value: %u\n", Atomic.load());
}
}
return 0;
}
// Output:
// progress with tag: min: 321.00us, avg: 493.84us, max: 1414.36us
// atomic value: 10002000
// progress no tag: min: 68.05us, avg: 123.58us, max: 2970.04us
// atomic value: 10002000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment