-
Notifications
You must be signed in to change notification settings - Fork 10
/
bench.cpp
146 lines (123 loc) · 3.31 KB
/
bench.cpp
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include <cstdlib>
#include <ctime>
#ifdef __clang__
#pragma clang diagnostic ignored "-Wtautological-constant-out-of-range-compare"
#endif
#include <rapidjson/document.h>
#include <picojson.h>
#include <rabbit.hpp>
void print(int score, const std::string& title, const std::string& url)
{
std::cerr << "(" << score << ") " << title << " - " << url << std::endl;
}
template <typename Bench>
struct runner
{
Bench bench;
std::string json;
int time;
int try_count;
runner(const std::string& json)
: json(json)
, time(0)
, try_count(0)
{}
void run(int n)
{
try
{
std::clock_t t = std::clock();
while (n-- > 0)
bench(json);
time += (std::clock() - t);
}
catch (std::exception& e)
{
std::cout << e.what() << std::endl;
}
++try_count;
}
void disp() const
{
std::cout << bench.name() << " " << "time: " << (time / try_count) << std::endl;
}
};
struct rapidjson_bench
{
std::string name() const { return "rapidjson"; }
void operator()(const std::string& json) const
{
rapidjson::Document doc;
if (doc.Parse<0>(json.c_str()).HasParseError())
throw std::runtime_error("parse_error");
const rapidjson::Value& children = doc["data"]["children"];
for (rapidjson::Value::ConstValueIterator it = children.Begin(); it != children.End(); ++it)
{
const rapidjson::Value& data = (*it)["data"];
print(data["score"].GetInt(), data["title"].GetString(), data["url"].GetString());
}
}
};
struct rabbit_bench
{
std::string name() const { return "rabbit "; }
void operator()(const std::string& json) const
{
rabbit::document doc;
try { doc.parse(json); } catch (...) { throw; }
rabbit::const_array children = doc["data"]["children"];
for (rabbit::array::const_iterator it = children.begin(); it != children.end(); ++it)
{
rabbit::const_object data = it->at("data");
print(data["score"].as(), data["title"].as(), data["url"].as());
}
}
};
struct picojson_bench
{
std::string name() const { return "picojson "; }
void operator()(const std::string& json) const
{
picojson::value v;
std::string error;
std::string::const_iterator it = json.begin();
picojson::parse(v, it, json.end(), &error);
if (!error.empty())
throw std::runtime_error(error);
const picojson::array& children = v.get("data").get("children").get<picojson::array>();
for (picojson::array::const_iterator it = children.begin(); it != children.end(); ++it)
{
const picojson::value& data = it->get("data");
print(data.get("score").get<double>(), data.get("title").get<std::string>(), data.get("url").get<std::string>());
}
}
};
int main(int argc, char** argv)
{
int n = 1000;
if (argc >= 2) n = std::atoi(argv[1]);
std::ifstream ifs("hot.json");
std::istreambuf_iterator<char> it(ifs), last;
std::string json = std::string(it, last);
runner<rapidjson_bench> r1(json);
runner<rabbit_bench> r2(json);
runner<picojson_bench> r3(json);
int i = 1;
while (true)
{
std::cout << i << " trying...";
r1.run(n);
r2.run(n);
r3.run(n);
std::cout << "OK" << std::endl;
r1.disp();
r2.disp();
r3.disp();
std::cout << "---" << std::endl;
++i;
}
}