#include <chrono>
#include <iostream>
#include <thread>
#include <utility>
/* динамически полиморфная иерархия */
struct DynaPolyBase // интерфейс
{
virtual void doIt() const = 0;
};
// реализации
struct DynaPolyDerived1 : DynaPolyBase
{
void doIt() const override {};
};
struct DynaPolyDerived2 : DynaPolyBase
{
void doIt() const override {};
};
struct DynaPolyDerived3 : DynaPolyDerived1
{
void doIt() const override {};
};
/* статически полиморфная иерархия */
// реализации
struct StatPolyDerived1
{
void doIt() const {};
};
struct StatPolyDerived2
{
void doIt() const {};
};
struct StatPolyDerived3
{
void doIt() const {};
};
template <typename Class> // интерфейс
struct StatPolyBase : Class
{
};
/* простой тестер */
template <typename C, auto Count = 1000u * 1000 * 1000>
void doIt(const C& c)
{
for (auto i = 0; i < Count; ++i) c.doIt();
}
int main()
{
StatPolyBase<StatPolyDerived2> item1;
DynaPolyDerived2 item2;
auto start = std::chrono::high_resolution_clock::now();
doIt(item1); // чек статики
auto diff = std::chrono::high_resolution_clock::now() - start;
std::cout << diff.count() << std::endl;
start = std::chrono::high_resolution_clock::now();
doIt(item2); // чек динамики
diff = std::chrono::high_resolution_clock::now() - start;
std::cout << diff.count() << std::endl;
}