summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Mittendrein <maxmitti@maxmitti.tk>2022-03-22 21:23:34 +0100
committerMarkus Mittendrein <maxmitti@maxmitti.tk>2022-03-22 21:28:37 +0100
commitd3ee8accd4fde07fbe313767c19c715b8d4ef359 (patch)
treed5fa8780e5ebfbd7b2569aac107048c5772c867b
parent15c989f527d33b2c3233d78e60b8c7a44f088732 (diff)
downloadcxxformat-d3ee8accd4fde07fbe313767c19c715b8d4ef359.tar.gz
cxxformat-d3ee8accd4fde07fbe313767c19c715b8d4ef359.zip
Add benchmark
-rw-r--r--CMakeLists.txt3
-rw-r--r--benchmark.cpp32
2 files changed, 35 insertions, 0 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 89be727..d064c3c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -33,3 +33,6 @@ add_custom_target(single-header DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/cxxformat.h
add_executable(test EXCLUDE_FROM_ALL main.cpp)
target_link_libraries(test PRIVATE cxxformat)
+
+add_executable(benchmark EXCLUDE_FROM_ALL benchmark.cpp)
+target_link_libraries(benchmark PRIVATE cxxformat)
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'; });
+}