Skip to content

Commit

Permalink
Add a perf-sanity test
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 14, 2024
1 parent f29a7e7 commit e10643a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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}
Expand Down
26 changes: 26 additions & 0 deletions test/perf-sanity.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// A quick and dirty performance test.
// For actual benchmarks see https://github.com/fmtlib/format-benchmark.

#include <atomic>
#include <chrono>
#include <iterator>

#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<double> 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);
}

0 comments on commit e10643a

Please sign in to comment.