Last active
April 2, 2022 17:52
-
-
Save Hexlord/2a97d3a1e6f16067ac78ed2e12f5eeb6 to your computer and use it in GitHub Desktop.
This file contains 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 "flecs.h" | |
#include "inttypes.h" | |
#include <atomic> | |
#include <cstdio> | |
#include <ctime> | |
constexpr int ENTITIES = 1000; | |
constexpr int MILLION = 1000000; | |
constexpr int SAMPLE_COUNT = 10000; | |
constexpr bool MILTI_THREADED = true; | |
constexpr float MOD_CHANCE = 0.000002f; | |
std::atomic<uint32_t> Atomic; | |
float RollModified() { return rand() / (float)RAND_MAX <= MOD_CHANCE; } | |
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; | |
srand(0); | |
ECS.system<Comp1>() | |
.multi_threaded(MILTI_THREADED) | |
.iter([&](flecs::iter &Iter) { | |
for (auto Index : Iter) { | |
auto Entity = Iter.entity(Index); | |
(void)Entity; | |
if (RollModified()) { | |
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; | |
srand(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; | |
if (RollModified()) { | |
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: 109.42us, avg: 190.92us, max: 12549.78us | |
// progress no tag: min: 107.00us, avg: 187.74us, max: 2717.46us |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment