Skip to content

Commit 20e27e5

Browse files
committed
add bench_uint16
Signed-off-by: Shikhar <shikharish05@gmail.com>
1 parent 011763f commit 20e27e5

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

benchmarks/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,22 @@ FetchContent_MakeAvailable(counters)
1111
add_executable(realbenchmark benchmark.cpp)
1212
target_link_libraries(realbenchmark PRIVATE counters::counters)
1313
add_executable(bench_ip bench_ip.cpp)
14+
add_executable(bench_uint16 bench_uint16.cpp)
1415
target_link_libraries(bench_ip PRIVATE counters::counters)
16+
target_link_libraries(bench_uint16 PRIVATE counters::counters)
1517

1618
set_property(
1719
TARGET realbenchmark
1820
PROPERTY CXX_STANDARD 17)
1921
set_property(
2022
TARGET bench_ip
2123
PROPERTY CXX_STANDARD 17)
24+
set_property(
25+
TARGET bench_uint16
26+
PROPERTY CXX_STANDARD 17)
2227
target_link_libraries(realbenchmark PUBLIC fast_float)
2328
target_link_libraries(bench_ip PUBLIC fast_float)
29+
target_link_libraries(bench_uint16 PUBLIC fast_float)
2430

2531
include(ExternalProject)
2632

benchmarks/bench_uint16.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include "counters/bench.h"
2+
#include "fast_float/fast_float.h"
3+
#include <charconv>
4+
#include <cstdint>
5+
#include <cstdio>
6+
#include <cstdlib>
7+
#include <cstring>
8+
#include <random>
9+
#include <atomic>
10+
#include <string>
11+
#include <vector>
12+
13+
void pretty_print(size_t volume, size_t bytes, std::string name,
14+
counters::event_aggregate agg) {
15+
if (agg.inner_count > 1) {
16+
printf("# (inner count: %d)\n", agg.inner_count);
17+
}
18+
printf("%-40s : ", name.c_str());
19+
printf(" %5.2f GB/s ", bytes / agg.fastest_elapsed_ns());
20+
printf(" %5.1f Mip/s ", volume * 1000.0 / agg.fastest_elapsed_ns());
21+
printf(" %5.2f ns/ip ", agg.fastest_elapsed_ns() / volume);
22+
if (counters::event_collector().has_events()) {
23+
printf(" %5.2f GHz ", agg.fastest_cycles() / agg.fastest_elapsed_ns());
24+
printf(" %5.2f c/ip ", agg.fastest_cycles() / volume);
25+
printf(" %5.2f i/ip ", agg.fastest_instructions() / volume);
26+
printf(" %5.2f c/b ", agg.fastest_cycles() / bytes);
27+
printf(" %5.2f i/b ", agg.fastest_instructions() / bytes);
28+
printf(" %5.2f i/c ", agg.fastest_instructions() / agg.fastest_cycles());
29+
}
30+
printf("\n");
31+
}
32+
33+
enum class parse_method { standard, fast_float };
34+
35+
void validate(const std::string &buffer, const std::vector<uint16_t> &expected,
36+
char delimiter) {
37+
const char *p = buffer.data();
38+
const char *pend = p + buffer.size();
39+
40+
for (size_t i = 0; i < expected.size(); i++) {
41+
uint16_t val;
42+
auto r = fast_float::from_chars(p, pend, val);
43+
if (r.ec != std::errc() || val != expected[i]) {
44+
printf("Validation failed at index %zu: expected %u, got %u\n", i,
45+
expected[i], val);
46+
std::abort();
47+
}
48+
p = r.ptr;
49+
if (i + 1 < expected.size()) {
50+
if (p >= pend || *p != delimiter) {
51+
printf("Validation failed at index %zu: delimiter mismatch\n", i);
52+
std::abort();
53+
}
54+
++p;
55+
}
56+
}
57+
58+
if (p != pend) {
59+
printf("Validation failed: trailing bytes remain\n");
60+
std::abort();
61+
}
62+
printf("Validation passed!\n");
63+
}
64+
65+
int main() {
66+
constexpr size_t N = 500000;
67+
constexpr char delimiter = ',';
68+
std::mt19937 rng(1234);
69+
std::uniform_int_distribution<int> dist(0, 65535);
70+
71+
std::vector<uint16_t> expected;
72+
expected.reserve(N);
73+
74+
std::string buffer;
75+
buffer.reserve(N * 6); // up to 5 digits + delimiter
76+
77+
for (size_t i = 0; i < N; ++i) {
78+
uint16_t val = (uint16_t)dist(rng);
79+
expected.push_back(val);
80+
std::string s = std::to_string(val);
81+
buffer.append(s);
82+
if (i + 1 < N) {
83+
buffer.push_back(delimiter);
84+
}
85+
}
86+
87+
size_t total_bytes = buffer.size();
88+
89+
validate(buffer, expected, delimiter);
90+
91+
volatile uint64_t sink = 0;
92+
93+
pretty_print(N, total_bytes, "parse_uint16_std_fromchars",
94+
counters::bench([&]() {
95+
uint64_t sum = 0;
96+
const char *p = buffer.data();
97+
const char *pend = p + buffer.size();
98+
for (size_t i = 0; i < N; ++i) {
99+
uint16_t value = 0;
100+
auto r = std::from_chars(p, pend, value);
101+
if (r.ec != std::errc())
102+
std::abort();
103+
sum += value;
104+
p = r.ptr;
105+
if (i + 1 < N) {
106+
if (p >= pend || *p != delimiter)
107+
std::abort();
108+
++p;
109+
}
110+
}
111+
if (p != pend)
112+
std::abort();
113+
sink += sum;
114+
}));
115+
116+
pretty_print(N, total_bytes, "parse_uint16_fastfloat", counters::bench([&]() {
117+
uint64_t sum = 0;
118+
const char *p = buffer.data();
119+
const char *pend = p + buffer.size();
120+
for (size_t i = 0; i < N; ++i) {
121+
uint16_t value = 0;
122+
auto r = fast_float::from_chars(p, pend, value);
123+
if (r.ec != std::errc())
124+
std::abort();
125+
sum += value;
126+
p = r.ptr;
127+
if (i + 1 < N) {
128+
if (p >= pend || *p != delimiter)
129+
std::abort();
130+
++p;
131+
}
132+
}
133+
if (p != pend)
134+
std::abort();
135+
sink += sum;
136+
}));
137+
138+
return EXIT_SUCCESS;
139+
}

0 commit comments

Comments
 (0)