diff --git a/CMakeLists.txt b/CMakeLists.txt index f3523668..7488a885 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -999,8 +999,12 @@ if(WITH_BENCHMARKS) list(APPEND BINARY_TARGETS dwarfs_benchmark) add_executable(multiversioning_benchmark test/multiversioning_benchmark.cpp) - target_link_libraries(multiversioning_benchmark PRIVATE dwarfs_test_helpers benchmark::benchmark) + target_link_libraries(multiversioning_benchmark PRIVATE benchmark::benchmark) list(APPEND BINARY_TARGETS multiversioning_benchmark) + + add_executable(converter_benchmark test/converter_benchmark.cpp) + target_link_libraries(converter_benchmark PRIVATE dwarfs_test_helpers benchmark::benchmark) + list(APPEND BINARY_TARGETS converter_benchmark) endif() add_library(dwarfs_follybenchmark_lite ${FOLLY_BENCHMARK_LITE_SRC}) diff --git a/test/converter_benchmark.cpp b/test/converter_benchmark.cpp new file mode 100644 index 00000000..393d1f5f --- /dev/null +++ b/test/converter_benchmark.cpp @@ -0,0 +1,246 @@ +/* vim:set ts=2 sw=2 sts=2 et: */ +/** + * \author Marcus Holland-Moritz (github@mhxnet.de) + * \copyright Copyright (c) Marcus Holland-Moritz + * + * This file is part of dwarfs. + * + * dwarfs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * dwarfs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with dwarfs. If not, see . + */ + +#include + +#include + +#include +#include +#include +#include +#include +#include + +// bug in boost: this should include make_default.hpp, but doesn't +#include + +namespace { + +std::array constexpr integer_strings = { + "0", "4711", "42", "1337", "1234567890", +}; + +std::array constexpr integer_strings_invalid = { + "a", "4711a", "42a", "1337a", "1234567890a", +}; + +std::array constexpr integer_values = {0, 4711, 42, 1337, 1234567890}; + +void int_to_string_folly_to(::benchmark::State& state) { + for (auto _ : state) { + for (auto i : integer_values) { + auto v = folly::to(i); + benchmark::DoNotOptimize(v); + } + } +} + +void int_to_string_lexical_cast(::benchmark::State& state) { + for (auto _ : state) { + for (auto i : integer_values) { + auto v = boost::lexical_cast(i); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_folly_to(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings) { + auto v = folly::to(s); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_lexical_cast(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings) { + auto v = boost::lexical_cast(s); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_folly_to_invalid(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings_invalid) { + auto v = folly::tryTo(s); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_lexical_cast_invalid(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings_invalid) { + try { + auto v = boost::lexical_cast(s); + benchmark::DoNotOptimize(v); + } catch (const boost::bad_lexical_cast&) { + } + } + } +} + +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" +#endif + +void int_to_string_convert_lexical_cast(::benchmark::State& state) { + for (auto _ : state) { + for (auto i : integer_values) { + auto v = + boost::convert(i, boost::cnv::lexical_cast()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void int_to_string_convert_cstream(::benchmark::State& state) { + for (auto _ : state) { + for (auto i : integer_values) { + auto v = boost::convert(i, boost::cnv::cstream()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void int_to_string_convert_spirit(::benchmark::State& state) { + for (auto _ : state) { + for (auto i : integer_values) { + auto v = boost::convert(i, boost::cnv::spirit()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void int_to_string_convert_charconv(::benchmark::State& state) { + for (auto _ : state) { + for (auto i : integer_values) { + auto v = boost::convert(i, boost::cnv::charconv()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_lexical_cast(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings) { + auto v = boost::convert(s, boost::cnv::lexical_cast()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_cstream(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings) { + auto v = boost::convert(s, boost::cnv::cstream()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_spirit(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings) { + auto v = boost::convert(s, boost::cnv::spirit()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_charconv(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings) { + auto v = boost::convert(s, boost::cnv::charconv()).value(); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_lexical_cast_invalid(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings_invalid) { + auto v = boost::convert(s, boost::cnv::lexical_cast()); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_cstream_invalid(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings_invalid) { + auto v = boost::convert(s, boost::cnv::cstream()); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_spirit_invalid(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings_invalid) { + auto v = boost::convert(s, boost::cnv::spirit()); + benchmark::DoNotOptimize(v); + } + } +} + +void string_to_int_convert_charconv_invalid(::benchmark::State& state) { + for (auto _ : state) { + for (auto s : integer_strings_invalid) { + auto v = boost::convert(s, boost::cnv::charconv()); + benchmark::DoNotOptimize(v); + } + } +} + +#if defined(__GNUC__) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif + +} // namespace + +BENCHMARK(int_to_string_folly_to); +BENCHMARK(int_to_string_lexical_cast); +BENCHMARK(int_to_string_convert_lexical_cast); +BENCHMARK(int_to_string_convert_cstream); +BENCHMARK(int_to_string_convert_spirit); +BENCHMARK(int_to_string_convert_charconv); + +BENCHMARK(string_to_int_folly_to); +BENCHMARK(string_to_int_lexical_cast); +BENCHMARK(string_to_int_convert_lexical_cast); +BENCHMARK(string_to_int_convert_cstream); +BENCHMARK(string_to_int_convert_spirit); +BENCHMARK(string_to_int_convert_charconv); + +BENCHMARK(string_to_int_folly_to_invalid); +BENCHMARK(string_to_int_lexical_cast_invalid); +BENCHMARK(string_to_int_convert_lexical_cast_invalid); +BENCHMARK(string_to_int_convert_cstream_invalid); +BENCHMARK(string_to_int_convert_spirit_invalid); +BENCHMARK(string_to_int_convert_charconv_invalid); + +BENCHMARK_MAIN();