Created
February 27, 2020 22:05
-
-
Save carlfindahl/b1e7d4f41fa1d90fdecb164894617f25 to your computer and use it in GitHub Desktop.
EnTT Meta vs. Unique ptr Vector
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 <cmath> | |
#include <vector> | |
#include <memory> | |
#include <benchmark/benchmark.h> | |
#include <entt/meta/factory.hpp> | |
/** Arbitrary system with some pointers to give it a size */ | |
class ISystem | |
{ | |
protected: | |
float* a = nullptr; | |
float* b = nullptr; | |
float* c = nullptr; | |
public: | |
virtual void update(float dt) = 0; | |
}; | |
class ActionSystem : public ISystem | |
{ | |
public: | |
void update(float dt) override; | |
}; | |
void ActionSystem::update(float dt) | |
{ | |
/** Do arbitrary math */ | |
float x = 10 / 10.f; | |
for (int i = 0; i < 100'000; ++i) | |
{ | |
x += std::sqrt(static_cast<float>(i)) * dt; | |
if (a == nullptr) | |
{ | |
b = &x; | |
} | |
} | |
float y = 5.f / x; | |
b = nullptr; | |
benchmark::DoNotOptimize(y); | |
} | |
class ActionNonVirtalSystem | |
{ | |
private: | |
float* a = nullptr; | |
float* b = nullptr; | |
float* c = nullptr; | |
public: | |
void update(float dt) | |
{ | |
/** Do arbitrary math */ | |
float x = 10 / 10.f; | |
for (int i = 0; i < 100'000; ++i) | |
{ | |
x += std::sqrt(static_cast<float>(i)) * dt; | |
if (a == nullptr) | |
{ | |
b = &x; | |
} | |
} | |
float y = 5.f / x; | |
b = nullptr; | |
benchmark::DoNotOptimize(y); | |
} | |
}; | |
static void bm_virtual_update(benchmark::State& state) | |
{ | |
/** Fill vector with N systems */ | |
std::vector<std::unique_ptr<ISystem>> vec{}; | |
for (int i = 0; i < 6; ++i) | |
{ | |
vec.emplace_back(new ActionSystem()); | |
} | |
/** Measure this */ | |
for (auto _ : state) | |
{ | |
for (auto&& system : vec) | |
{ | |
system->update(1.f / 60.f); | |
} | |
} | |
} | |
static void bm_meta_update(benchmark::State& state) | |
{ | |
/** Reflect type */ | |
entt::meta<ActionNonVirtalSystem>() | |
.type(entt::hashed_string("ActionSystem")) | |
.ctor<>() | |
.func<&ActionNonVirtalSystem::update>(entt::hashed_string("update")); | |
/** Fill vector with N systems */ | |
std::vector<entt::meta_any> vec{}; | |
for (int i = 0; i < state.range(0); ++i) | |
{ | |
auto type = entt::resolve(entt::hashed_string("ActionSystem")); | |
vec.push_back(type.construct()); | |
} | |
/** Measure this */ | |
for (auto _ : state) | |
{ | |
for (auto&& system : vec) | |
{ | |
system.type().func(entt::hashed_string("update")).invoke(system, 1.f / 60.f); | |
} | |
} | |
} | |
BENCHMARK(bm_virtual_update)->RangeMultiplier(2)->Range(4, 128); | |
BENCHMARK(bm_meta_update)->RangeMultiplier(2)->Range(4, 128); | |
BENCHMARK_MAIN(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment