Skip to content

Instantly share code, notes, and snippets.

@Hexlord
Created April 1, 2022 23:46
Show Gist options
  • Save Hexlord/60e00499114b30ee4dd592eac86d8cd7 to your computer and use it in GitHub Desktop.
Save Hexlord/60e00499114b30ee4dd592eac86d8cd7 to your computer and use it in GitHub Desktop.
#include "flecs.h"
#include "inttypes.h"
#include <cstdio>
#include <ctime>
auto nanos() {
timespec Time;
clock_gettime(CLOCK_MONOTONIC, &Time);
return Time.tv_sec * int64_t{1000000000} + (int64_t)Time.tv_nsec;
}
#define ENTITIES 100
int main() {
struct Comp1 {
int field1;
int field2;
};
struct Comp2 {
int field1;
int field2;
};
struct Comp3 {
int field1;
int field2;
};
float Baseline;
{
// Warm-up
flecs::world ECS;
ECS.progress();
auto Start = nanos();
ECS.progress();
auto End = nanos();
Baseline = (End - Start) / 1000000000.0f;
}
{
flecs::world ECS;
volatile uint32_t Count = 0;
ECS.system<Comp1, Comp2, Comp3>().iter([&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
for (int I = 0; I < 100000; ++I) {
Count = (Count + 5) * Count;
}
}
});
ECS.system<Comp1, Comp2, Comp3>().iter([&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
for (int I = 0; I < 100000; ++I) {
Count = (Count + 5) * Count;
}
}
});
auto Start0 = nanos();
for (int Index = 0; Index < ENTITIES; ++Index) {
auto Parent = ECS.entity(100000 + Index);
ECS.scope(Parent, [&]() {
ECS.entity(1000 + Index).add<Comp1>().add<Comp2>().add<Comp3>();
});
}
auto End0 = nanos();
ECS.progress();
auto End1 = nanos();
ECS.progress();
auto End2 = nanos();
printf("test 1 took %.3fms (setup) + %.3fms (progress 1) + %.3fms "
"(progress 2)\n",
((End0 - Start0) / 1000000000.0f - Baseline) * 1000.0f,
((End1 - End0) / 1000000000.0f - Baseline) * 1000.0f,
((End2 - End1) / 1000000000.0f - Baseline) * 1000.0f);
}
{
flecs::world ECS;
ECS.set_threads(8);
volatile uint32_t Count = 0;
ECS.system<Comp1, Comp2, Comp3>().iter([&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
for (int I = 0; I < 100000; ++I) {
Count = (Count + 5) * Count;
}
}
});
ECS.system<Comp1, Comp2, Comp3>().iter([&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
for (int I = 0; I < 100000; ++I) {
Count = (Count + 5) * Count;
}
}
});
auto Start0 = nanos();
for (int Index = 0; Index < ENTITIES; ++Index) {
auto Parent = ECS.entity(100000 + Index);
ECS.scope(Parent, [&]() {
ECS.entity(1000 + Index).add<Comp1>().add<Comp2>().add<Comp3>();
});
}
auto End0 = nanos();
ECS.progress();
auto End1 = nanos();
ECS.progress();
auto End2 = nanos();
printf("test 2 took %.3fms (setup) + %.3fms (progress 1) + %.3fms "
"(progress 2)\n",
((End0 - Start0) / 1000000000.0f - Baseline) * 1000.0f,
((End1 - End0) / 1000000000.0f - Baseline) * 1000.0f,
((End2 - End1) / 1000000000.0f - Baseline) * 1000.0f);
}
{
flecs::world ECS;
ECS.set_threads(8);
volatile uint32_t Count = 0;
ECS.system<Comp1, Comp2, Comp3>().multi_threaded().iter(
[&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
for (int I = 0; I < 100000; ++I) {
Count = (Count + 5) * Count;
}
}
});
ECS.system<Comp1, Comp2, Comp3>().multi_threaded().iter(
[&](flecs::iter &Iter) {
for (auto Index : Iter) {
auto Entity = Iter.entity(Index);
(void)Entity;
for (int I = 0; I < 100000; ++I) {
Count = (Count + 5) * Count;
}
}
});
auto Start0 = nanos();
for (int Index = 0; Index < ENTITIES; ++Index) {
auto Parent = ECS.entity(100000 + Index);
ECS.scope(Parent, [&]() {
ECS.entity(1000 + Index).add<Comp1>().add<Comp2>().add<Comp3>();
});
}
auto End0 = nanos();
ECS.progress();
auto End1 = nanos();
ECS.progress();
auto End2 = nanos();
printf("test 3 took %.3fms (setup) + %.3fms (progress 1) + %.3fms "
"(progress 2)\n",
((End0 - Start0) / 1000000000.0f - Baseline) * 1000.0f,
((End1 - End0) / 1000000000.0f - Baseline) * 1000.0f,
((End2 - End1) / 1000000000.0f - Baseline) * 1000.0f);
}
return 0;
}
/home/sasha/flecs_test/cmake-build-release/SECore
test 1 took 0.057ms (setup) + 48.122ms (progress 1) + 47.668ms (progress 2)
test 2 took 0.050ms (setup) + 47.872ms (progress 1) + 49.861ms (progress 2)
test 3 took 0.063ms (setup) + 23.187ms (progress 1) + 22.701ms (progress 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment