Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 637b088

Browse files
committed
Update c++ to print throughput in MB/s instead of ms
1 parent 9c93da0 commit 637b088

File tree

3 files changed

+33
-16
lines changed

3 files changed

+33
-16
lines changed

cpp/main.cc

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,24 @@ void bench(const char* path) {
2929
in.read(&s[0], s.size());
3030
in.close();
3131
auto c_str = s.c_str();
32+
auto bytes = s.size();
3233

3334
Timer timer;
3435

3536
{
36-
auto min = std::numeric_limits<float>::infinity();
37+
std::chrono::microseconds min{std::numeric_limits<std::chrono::microseconds::rep>::max()};
3738
for (int i = 0; i < 256; i++) {
3839
rapidjson::Document d;
3940
timer.reset();
4041
d.Parse(c_str);
4142
assert(!d.HasParseError());
42-
min = std::min(min, timer.millis());
43+
min = std::min(min, timer.micros());
4344
}
44-
std::cout << path << " parse dom: " << min << "ms" << std::endl;
45+
std::cout << path << " parse dom: " << throughput(min, bytes) << " MB/s" << std::endl;
4546
}
4647

4748
{
48-
auto min = std::numeric_limits<float>::infinity();
49+
std::chrono::microseconds min{std::numeric_limits<std::chrono::microseconds::rep>::max()};
4950
rapidjson::Document d;
5051
d.Parse(c_str);
5152
assert(!d.HasParseError());
@@ -56,13 +57,13 @@ void bench(const char* path) {
5657
timer.reset();
5758
auto result = d.Accept(writer);
5859
assert(result);
59-
min = std::min(min, timer.millis());
60+
min = std::min(min, timer.micros());
6061
}
61-
std::cout << path << " stringify dom: " << min << "ms" << std::endl;
62+
std::cout << path << " stringify dom: " << throughput(min, bytes) << " MB/s" << std::endl;
6263
}
6364

6465
{
65-
auto min = std::numeric_limits<float>::infinity();
66+
std::chrono::microseconds min{std::numeric_limits<std::chrono::microseconds::rep>::max()};
6667
for (int i = 0; i < 256; i++) {
6768
rapidjson::Reader reader;
6869
Handler handler;
@@ -79,13 +80,13 @@ void bench(const char* path) {
7980
#endif
8081
assert(result);
8182
auto keep = handler.Get();
82-
min = std::min(min, timer.millis());
83+
min = std::min(min, timer.micros());
8384
}
84-
std::cout << path << " parse struct: " << min << "ms" << std::endl;
85+
std::cout << path << " parse struct: " << throughput(min, bytes) << " MB/s" << std::endl;
8586
}
8687

8788
{
88-
auto min = std::numeric_limits<float>::infinity();
89+
std::chrono::microseconds min{std::numeric_limits<std::chrono::microseconds::rep>::max()};
8990
rapidjson::Reader reader;
9091
Handler handler;
9192
rapidjson::StringStream ss(c_str);
@@ -99,9 +100,9 @@ void bench(const char* path) {
99100
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
100101
timer.reset();
101102
Serialize(d, writer);
102-
min = std::min(min, timer.millis());
103+
min = std::min(min, timer.micros());
103104
}
104-
std::cout << path << " stringify struct: " << min << "ms" << std::endl;
105+
std::cout << path << " stringify struct: " << throughput(min, bytes) << " MB/s" << std::endl;
105106
}
106107
}
107108

cpp/timer.cc

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ void Timer::reset() {
66
begin_ = clock_::now();
77
}
88

9-
float Timer::millis() const {
10-
return std::chrono::duration_cast<milli_>(clock_::now() - begin_).count();
9+
std::chrono::microseconds Timer::micros() const {
10+
return std::chrono::duration_cast<std::chrono::microseconds>(clock_::now() - begin_);
11+
}
12+
13+
// matches json-benchmark/src/lib.rs
14+
uint64_t throughput(std::chrono::microseconds dur, size_t bytes) {
15+
auto mbps = bytes / static_cast<uint64_t>(dur.count());
16+
17+
// Round to two significant digits.
18+
if (mbps > 100) {
19+
if (mbps % 10 >= 5) {
20+
mbps += 10;
21+
}
22+
mbps = mbps / 10 * 10;
23+
}
24+
25+
return mbps;
1126
}

cpp/timer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ class Timer {
77
public:
88
Timer();
99
void reset();
10-
float millis() const;
10+
std::chrono::microseconds micros() const;
1111

1212
private:
1313
typedef std::chrono::high_resolution_clock clock_;
14-
typedef std::chrono::duration<float, std::milli> milli_;
1514
std::chrono::time_point<clock_> begin_;
1615
};
1716

17+
uint64_t throughput(std::chrono::microseconds dur, size_t bytes);
18+
1819
#endif // TIMER_H_

0 commit comments

Comments
 (0)