From e10643add21e9c35d41f60288f13352da754d2b9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 14 Jul 2024 14:17:39 -0700 Subject: [PATCH] Add a perf-sanity test --- test/CMakeLists.txt | 5 ++++- test/perf-sanity.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 test/perf-sanity.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index fac82392..a14dfc24 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -110,6 +110,9 @@ add_fmt_test(enforce-checks-test) target_compile_definitions(enforce-checks-test PRIVATE -DFMT_ENFORCE_COMPILE_STRING) +add_executable(perf-sanity perf-sanity.cc) +target_link_libraries(perf-sanity fmt::fmt) + if (FMT_MODULE) # The tests need {fmt} to be compiled as traditional library # because of visibility of implementation details. @@ -233,7 +236,7 @@ if (FMT_PEDANTIC AND NOT WIN32 AND NOT ( "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") endif () -# This test are disabled on Windows because it is only *NIX issue. +# This test is disabled on Windows because it is POSIX-specific. if (FMT_PEDANTIC AND NOT WIN32) add_test(static-export-test ${CMAKE_CTEST_COMMAND} -C ${CMAKE_BUILD_TYPE} diff --git a/test/perf-sanity.cc b/test/perf-sanity.cc new file mode 100644 index 00000000..7696b2f1 --- /dev/null +++ b/test/perf-sanity.cc @@ -0,0 +1,26 @@ +// A quick and dirty performance test. +// For actual benchmarks see https://github.com/fmtlib/format-benchmark. + +#include +#include +#include + +#include "fmt/format.h" + +int main() { + const int n = 10000000; + + auto start = std::chrono::steady_clock::now(); + for (int iteration = 0; iteration < n; ++iteration) { + auto buf = fmt::memory_buffer(); + fmt::format_to(std::back_inserter(buf), + "Hello, {}. The answer is {} and {}.", 1, 2345, 6789); + } + std::atomic_signal_fence(std::memory_order_acq_rel); // Clobber memory. + auto end = std::chrono::steady_clock::now(); + + std::chrono::duration duration = end - start; + double total_time = duration.count() * 1000; // Convert to milliseconds. + fmt::print("Total time for formatting {} strings: {:.1f} ms.\n", n, + total_time); +}