[libcxx-commits] [libcxx] [libc++][format][1/7] Adds more benchmarks. (PR #101803)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Aug 13 08:55:09 PDT 2024


================
@@ -0,0 +1,106 @@
+//===----------------------------------------------------------------------===//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include <array>
+#include <charconv>
+#include <cstdio>
+#include <format>
+#include <iterator>
+#include <list>
+#include <random>
+#include <vector>
+
+#include "benchmark/benchmark.h"
+
+std::array data = [] {
+  std::uniform_real_distribution<double> distribution;
+  std::mt19937 generator;
+  std::array<double, 1000> result;
+  std::generate_n(result.begin(), result.size(), [&] { return distribution(generator); });
+  return result;
+}();
+
+static void BM_sprintf(benchmark::State& state) {
+  std::array<char, 100> output;
+  while (state.KeepRunningBatch(data.size()))
+    for (auto value : data)
+      benchmark::DoNotOptimize(sprintf(output.data(), "%f", value));
----------------
ldionne wrote:

I think you want

```
sprintf(output.data(), "%f", value);
benchmark::DoNotOptimize(output.data());
```

https://github.com/llvm/llvm-project/pull/101803


More information about the libcxx-commits mailing list