1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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'; });
}
|