summaryrefslogtreecommitdiffstats
path: root/benchmark.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'benchmark.cpp')
-rw-r--r--benchmark.cpp32
1 files changed, 32 insertions, 0 deletions
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 <chrono>
+#include <cxxformat/cxxformat>
+
+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<char, 100>{}](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'; });
+}