-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_simdjson.cc
56 lines (45 loc) · 1.87 KB
/
benchmark_simdjson.cc
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
#include <simdjson.h>
#include <fstream>
#include <sstream>
#include <iostream>
#include <chrono>
#include <filesystem>
int main(int argc, char **argv) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <json_file>\n";
return 1;
}
try {
const std::filesystem::path input_file{argv[1]};
// Parse
simdjson::ondemand::parser parser;
const auto timestamp_parse_start{std::chrono::high_resolution_clock::now()};
simdjson::padded_string json = simdjson::padded_string::load(input_file.string());
auto doc = parser.iterate(json);
// Force evaluation of every value
auto array = doc.get_array();
std::size_t count{0};
for (auto number : array) {
count += 1;
}
const auto timestamp_parse_end{std::chrono::high_resolution_clock::now()};
// Stringify
const auto timestamp_stringify_start{std::chrono::high_resolution_clock::now()};
std::string output(json.data(), json.length());
const auto timestamp_stringify_end{std::chrono::high_resolution_clock::now()};
const auto duration_parse{std::chrono::duration_cast<std::chrono::microseconds>(
timestamp_parse_end - timestamp_parse_start)};
const auto duration_stringify{std::chrono::duration_cast<std::chrono::microseconds>(
timestamp_stringify_end - timestamp_stringify_start)};
std::cerr << "Parse time: " << duration_parse.count() << "µs\n";
std::cerr << "Stringify time: " << duration_stringify.count() << "µs\n";
std::cout << output << "\n";
return 0;
} catch (const simdjson::simdjson_error &e) {
std::cerr << "JSON Error: " << e.what() << "\n";
return 1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}