|
| 1 | +#include <fast_io.h> |
| 2 | +#include <fast_io_device.h> |
| 3 | +#include <fast_io_driver/timer.h> |
| 4 | +#include <vector> |
| 5 | +#include <string> |
| 6 | +#include </home/puji/fast_io/include/fast_io_dsal/string.h> |
| 7 | +#include <charconv> |
| 8 | +#include <fast_float/fast_float.h> |
| 9 | + |
| 10 | +using namespace fast_io::io; |
| 11 | + |
| 12 | +static std::string make_numbers_buffer(std::size_t n) |
| 13 | +{ |
| 14 | + std::string s; |
| 15 | + s.reserve(n * 8); |
| 16 | + for (std::size_t i{}; i != n; ++i) |
| 17 | + { |
| 18 | + auto old = s.size(); |
| 19 | + s.resize(old + 32); |
| 20 | + auto *first = s.data() + old; |
| 21 | + auto *last = s.data() + s.size(); |
| 22 | + auto res = std::to_chars(first, last - 1, i); |
| 23 | + *res.ptr = '\n'; |
| 24 | + s.resize(res.ptr - s.data() + 1); |
| 25 | + } |
| 26 | + return s; |
| 27 | +} |
| 28 | + |
| 29 | +int main() |
| 30 | +{ |
| 31 | + constexpr std::size_t N = 10'000'000; |
| 32 | + auto buf = make_numbers_buffer(N); |
| 33 | + char const *begin = buf.data(); |
| 34 | + char const *end = buf.data() + buf.size(); |
| 35 | + |
| 36 | + { |
| 37 | + std::size_t lines{}; |
| 38 | + for (char const *p = begin; p < end; ++p) |
| 39 | + { |
| 40 | + lines += (*p == '\n'); |
| 41 | + } |
| 42 | + fast_io::println("lines=", lines); |
| 43 | + } |
| 44 | + |
| 45 | + // atoi |
| 46 | + { |
| 47 | + fast_io::timer t(u8"atoi"); |
| 48 | + std::uint64_t sum{}; |
| 49 | + char const *p = begin; |
| 50 | + while (p < end) |
| 51 | + { |
| 52 | + int v = std::atoi(p); |
| 53 | + sum += static_cast<std::uint64_t>(v); |
| 54 | + while (p < end && *p >= '0' && *p <= '9') |
| 55 | + { |
| 56 | + ++p; |
| 57 | + } |
| 58 | + if (p < end && *p == '\n') |
| 59 | + { |
| 60 | + ++p; |
| 61 | + } |
| 62 | + } |
| 63 | + std::uint64_t volatile sink = sum; |
| 64 | + (void)sink; |
| 65 | + } |
| 66 | + |
| 67 | + // std::from_chars |
| 68 | + { |
| 69 | + fast_io::timer t(u8"std_from_chars"); |
| 70 | + std::uint64_t sum{}; |
| 71 | + char const *p = begin; |
| 72 | + while (p < end) |
| 73 | + { |
| 74 | + std::uint64_t v{}; |
| 75 | + auto res = std::from_chars(p, end, v); |
| 76 | + sum += v; |
| 77 | + p = res.ptr; |
| 78 | + if (p < end && *p == '\n') |
| 79 | + { |
| 80 | + ++p; |
| 81 | + } |
| 82 | + } |
| 83 | + std::uint64_t volatile sink = sum; |
| 84 | + (void)sink; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + // fast_io char_digit_to_literal |
| 89 | + { |
| 90 | + fast_io::timer t(u8"fastio_char_digit_to_literal"); |
| 91 | + std::uint64_t sum{}; |
| 92 | + char const *p = begin; |
| 93 | + while (p < end) |
| 94 | + { |
| 95 | + using UCh = std::make_unsigned_t<char>; |
| 96 | + std::uint64_t v{}; |
| 97 | + char const *q = p; |
| 98 | + while (q < end && *q != '\n') |
| 99 | + { |
| 100 | + UCh ch = static_cast<UCh>(*q); |
| 101 | + if (fast_io::details::char_digit_to_literal<10, char>(ch)) |
| 102 | + { |
| 103 | + break; |
| 104 | + } |
| 105 | + v = v * 10 + static_cast<std::uint64_t>(ch); |
| 106 | + ++q; |
| 107 | + } |
| 108 | + sum += v; |
| 109 | + p = (q < end ? q + 1 : q); |
| 110 | + } |
| 111 | + std::uint64_t volatile sink = sum; |
| 112 | + (void)sink; |
| 113 | + } |
| 114 | + |
| 115 | + // fast_float |
| 116 | + { |
| 117 | + fast_io::timer t(u8"fast_float_from_chars"); |
| 118 | + std::uint64_t sum{}; |
| 119 | + char const *p = begin; |
| 120 | + while (p < end) |
| 121 | + { |
| 122 | + std::uint64_t v{}; |
| 123 | + auto res = fast_float::from_chars(p, end, v); |
| 124 | + sum += v; |
| 125 | + p = res.ptr; |
| 126 | + if (p < end && *p == '\n') |
| 127 | + { |
| 128 | + ++p; |
| 129 | + } |
| 130 | + } |
| 131 | + std::uint64_t volatile sink = sum; |
| 132 | + (void)sink; |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments