From d3ee8accd4fde07fbe313767c19c715b8d4ef359 Mon Sep 17 00:00:00 2001 From: Markus Mittendrein Date: Tue, 22 Mar 2022 21:23:34 +0100 Subject: Add benchmark --- CMakeLists.txt | 3 +++ benchmark.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 benchmark.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 89be727..d064c3c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,3 +33,6 @@ add_custom_target(single-header DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/cxxformat.h add_executable(test EXCLUDE_FROM_ALL main.cpp) target_link_libraries(test PRIVATE cxxformat) + +add_executable(benchmark EXCLUDE_FROM_ALL benchmark.cpp) +target_link_libraries(benchmark PRIVATE cxxformat) diff --git a/benchmark.cpp b/benchmark.cpp new file mode 100644 index 0000000..7a7aeda --- /dev/null +++ b/benchmark.cpp @@ -0,0 +1,32 @@ +#include +#include + +using namespace format::literals; +void test(std::string_view name, auto what) +{ + using clock = std::chrono::steady_clock; + using namespace std::chrono_literals; + + const auto start = clock::now(); + + for (std::size_t i = 0; i < 10'000'000; ++i) + { + what(i); + } + + const auto stop = clock::now(); + + "%s took: %fs\n"_format_to(stderr, name, (stop - start) / 1.s); +} + +int main() +{ + test("_format_to(stdout)", [](auto i){ "%u\n"_format_to(stdout, i); }); + test("snprintf", [buf = std::array{}](auto i) mutable { snprintf(buf.data(), buf.size(), "%zu\n", i); }); + test("_format", [](auto i){ "%u\n"_format(i); }); + test("_format_to(std::cout)", [](auto i){ "%u\n"_format_to(std::cout, i); }); + test("run_time::format_template", [tmpl = format::run_time::format_template{"%u\n"}](auto i){ tmpl(stdout, i); }); + test("format_to(stdout)", [](auto i){ format::format_to("%u\n", format::NullOutput{}, i); }); + test("printf", [](auto i){ printf("%zu\n", i); }); + test("std::cout", [](auto i){ std::cout << i << '\n'; }); +} -- cgit v1.2.3-54-g00ecf