-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
80 lines (72 loc) · 2.38 KB
/
Copy pathmain.cpp
File metadata and controls
80 lines (72 loc) · 2.38 KB
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
#include "json_reader.hpp"
#include "nlohmann/nlohmann_json.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
#include <tuple>
using namespace std::chrono;
using namespace JsonDerulo;
#define TRIES 20
void time_taken(std::string path) {
long long total = 0;
for (int i = 0; i < TRIES; i++) {
std::ifstream ifs(path);
auto start = high_resolution_clock::now();
auto js = read_json(ifs);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
total += duration.count();
}
long long average_1 = total / TRIES;
total = 0;
for (int i = 0; i < TRIES; i++) {
std::ifstream ifs2(path);
auto start = high_resolution_clock::now();
auto j = nlohmann::json::parse(ifs2);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
total += duration.count();
}
long long average_2 = total / TRIES;
std::string unit = "us";
if (average_1 > 10000 || average_2 > 10000) {
average_1 /= 1000;
average_2 /= 1000;
unit = "ms";
}
std::cout << "jsonDerulo: " << average_1 << unit << std::endl;
std::cout << "Nlohmann JSON: " << average_2 << unit << std::endl;
}
void benchmark() {
std::cout << "Benchmarking..." << std::endl;
std::cout << "400 lines:" << std::endl;
time_taken("400.json");
std::cout << "700 lines:" << std::endl;
time_taken("700.json");
std::cout << "2000 lines:" << std::endl;
time_taken("2000.json");
std::cout << "5000 lines" << std::endl;
time_taken("5000.json");
std::cout << "10000 lines" << std::endl;
time_taken("10000.json");
}
int main(int argc, char **argv) {
// benchmark();
std::ifstream ifs("simple.json");
auto js = read_json(ifs).value();
auto nested = js["nested"];
auto nested_obj = nested.cast<Object>();
std::cout << *nested_obj->get<std::string>("foo") << std::endl;
auto nested2 = js.root.get<Object>("nested");
std::cout << "same obj? " << ((nested2 == nested_obj) ? "true" : "false")
<< std::endl;
js.root.insert("new_key", Value("new_value")); // insert a new key-value pair
Object obj;
obj.insert("key", Value("value"));
obj.insert("key2", Value(2));
js.root.insert(
"new_obj",
obj); // insert a new key-value pair with an object as the value
std::cout << js << std::endl;
std::cout << js.root["new_obj"]["key2"] << std::endl; // access a value
}