xref: /aosp_15_r20/external/fmtlib/test/perf-sanity.cc (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1 // A quick and dirty performance test.
2 // For actual benchmarks see https://github.com/fmtlib/format-benchmark.
3 
4 #include <atomic>
5 #include <chrono>
6 #include <iterator>
7 
8 #include "fmt/format.h"
9 
main()10 int main() {
11   const int n = 10000000;
12 
13   auto start = std::chrono::steady_clock::now();
14   for (int iteration = 0; iteration < n; ++iteration) {
15     auto buf = fmt::memory_buffer();
16     fmt::format_to(std::back_inserter(buf),
17                    "Hello, {}. The answer is {} and {}.", 1, 2345, 6789);
18   }
19   std::atomic_signal_fence(std::memory_order_acq_rel);  // Clobber memory.
20   auto end = std::chrono::steady_clock::now();
21 
22   // Print time in milliseconds.
23   std::chrono::duration<double> duration = end - start;
24   fmt::print("{:.1f}\n", duration.count() * 1000);
25 }
26