#include #include #include #include #include #include #include namespace { template TVector SelectValues(size_t count) { auto values = GetEnumAllValues().Materialize(); SortBy(values, [](const TEnum& v) { return IntHash(static_cast(v)); }); values.crop(count); return values; } template TVector SelectStrings(size_t count) { TVector strings(Reserve(GetEnumItemsCount())); for (const auto& [_, s] : GetEnumNames()) { strings.push_back(s); } SortBy(strings, [](const TStringBuf& s) { return THash()(s); }); strings.crop(count); return strings; } template void BMToString(TContext& iface) { const auto values = SelectValues(5u); for (const auto iter : xrange(iface.Iterations())) { Y_UNUSED(iter); for (const auto value : values) { Y_DO_NOT_OPTIMIZE_AWAY(ToString(value).size()); } } } template void BMOut(TContext& iface) { const auto values = SelectValues(5u); TNullOutput null; for (const auto iter : xrange(iface.Iterations())) { Y_UNUSED(iter); for (const auto value : values) { Y_DO_NOT_OPTIMIZE_AWAY(null << value); } } } template void BMFromString(TContext& iface) { const auto strings = SelectStrings(5u); for (const auto iter : xrange(iface.Iterations())) { Y_UNUSED(iter); for (const auto s : strings) { Y_DO_NOT_OPTIMIZE_AWAY(FromString(s)); } } } template void BMTryFromString(TContext& iface) { auto strings = SelectStrings(5u); strings.back() = "fake"; TEnum value; for (const auto iter : xrange(iface.Iterations())) { Y_UNUSED(iter); for (const auto s : strings) { Y_DO_NOT_OPTIMIZE_AWAY(TryFromString(s, value)); } } } } #define DEFINE_BENCHMARK(name) \ Y_CPU_BENCHMARK(ToString_##name, iface) { \ BMToString(iface); \ } \ Y_CPU_BENCHMARK(Out_##name, iface) { \ BMOut(iface); \ } \ Y_CPU_BENCHMARK(FromString_##name, iface) { \ BMFromString(iface); \ } \ Y_CPU_BENCHMARK(TryFromString_##name, iface) { \ BMTryFromString(iface); \ } DEFINE_BENCHMARK(ESmallSortedEnum); DEFINE_BENCHMARK(ESmalUnsortedEnum); DEFINE_BENCHMARK(EBigSortedEnum); DEFINE_BENCHMARK(EBigUnsortedEnum); DEFINE_BENCHMARK(EBigUnsortedDenseEnum);