#include #include #include #include #include #include "entities.hpp" #include #include using TestFunc = std::function; constexpr unsigned int testCount = 100; void test(const std::string& t, TestFunc ours, TestFunc theirs) { auto run = [](auto f) { clock_t avg = 0; for (unsigned int i = 0; i < testCount; i++) { auto start = clock(); f(); auto diff = clock() - start; avg += diff; } auto diff = avg / testCount; std::cout << "Test took " << diff << " ticks (" << diff / static_cast(CLOCKS_PER_SEC) << "ms).\n"; return diff; }; std::cout << std::fixed << std::setprecision(6); std::cout << "==============================================\n"; std::cout << "Running test: " << t << "\n"; std::cout << "Ours:\n"; auto o = run(ours); std::cout << "Theirs:\n"; auto p = run(theirs); auto percent = (o - p) / static_cast(p); std::cout << "\nDIFFERENCE: " << o - p << " ticks (" << std::setprecision(2) << percent << "%).\n\n"; } struct Position : public Component { float x, y; Position(float _x = 0, float _y = 0) : x(_x), y(_y) {} }; struct Velocity : public Component { float x, y; Velocity(float _x = 0, float _y = 0) : x(_x), y(_y) {} }; int main(void) { EntityManager em; entityx::EventManager ev; entityx::EntityManager xm (ev); entityx::SystemManager xsm (xm, ev); test("Creation500", [&em](){ for (int i = 0; i < 500; i++) em.create(); }, [&xm](){ for (int i = 0; i < 500; i++) xm.create(); }); test("Reset", [&em](){ em.reset(); }, [&xm](){ xm.reset(); }); test("Assign", [&em](){ auto e = em.create(); auto& pos = *e.assign(12, 34); //std::cout << pos.x << ' ' << pos.y << '\n'; pos.x = 56, pos.y = 16; //std::cout << pos.x << ' ' << pos.y << '\n'; }, [&xm](){ auto e = xm.create(); auto pos = e.assign(12, 34); //std::cout << pos->x << ' ' << pos->y << '\n'; pos->x = 56, pos->y = 16; //std::cout << pos->x << ' ' << pos->y << '\n'; }); test("HasComponent", [&em](){ auto e = em.create(); //std::cout << e.hasComponent() << '\n'; e.assign(); //std::cout << e.hasComponent() << '\n'; }, [&xm](){ auto e = xm.create(); //std::cout << e.has_component() << '\n'; e.assign(); //std::cout << e.has_component() << '\n'; }); test("Remove", [&em](){ auto e = em.create(); //std::cout << e.hasComponent() << '\n'; e.assign(); //std::cout << e.hasComponent() << '\n'; e.remove(); //std::cout << e.hasComponent() << '\n'; }, [&xm](){ auto e = xm.create(); //std::cout << e.has_component() << '\n'; e.assign(); //std::cout << e.has_component() << '\n'; e.remove(); //std::cout << e.has_component() << '\n'; }); /*test("EachAll", [&em](){ em.reset(); em.create(), em.create(), em.create(); int i = 0; em.each([&i](Entity e) { std::cout << ++i << '\n'; }); });*/ test("EachSpec", [&em](){ em.reset(); em.create(), em.create(); auto e1 = em.create(); e1.assign(1, 3); em.create(); auto e2 = em.create(); e2.assign(99); em.each([](Entity e) { auto& pos = *e.component(); //std::cout << pos.x << ' ' << pos.y << '\n'; }); }, [&xm](){ xm.reset(); xm.create(), xm.create(); auto e1 = xm.create(); e1.assign(1, 3); xm.create(); auto e2 = xm.create(); e2.assign(99); xm.each([](entityx::Entity e, Position& pos) { //std::cout << pos.x << ' ' << pos.y << '\n'; }); }); test("Loop100", [&em](){ for (int i = 100; i--;) { auto e = em.create(); e.assign(i, i); } em.each([](Entity e) { auto& pos = * e.component(); pos.x += 5, pos.y -= 5; }); }, [&xm](){ for (int i = 100; i--;) { auto e = xm.create(); e.assign(i, i); } xm.each([](entityx::Entity e, Position pos) { pos.x += 5, pos.y -= 5; }); }); /*test("Dependency", [&em](){ em.addDependency(); auto e = em.create(); e.assign(); std::cout << e.hasComponent() << e.hasComponent() << '\n'; }, [&xm, &xsm](){ xsm.add>(); auto e = xm.create(); e.assign(); std::cout << e.has_component() << e.has_component() << '\n'; });*/ return 0; }