Skip to content

Commit

Permalink
parse benchmark vs from_json
Browse files Browse the repository at this point in the history
  • Loading branch information
mymartin committed Dec 10, 2022
1 parent f9df3c4 commit 2ec4d0e
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions benchmark/benchmark.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "iguana/json_reader.hpp"
#include "iguana/json_writer.hpp"
#include "iguana/value.hpp"
#include <chrono>
#include <iostream>
#include <map>
Expand All @@ -24,7 +25,8 @@ class ScopedTimer {
if (m_ns)
*m_ns = dur.count();
else
std::cout << m_name << " : " << dur.count() << " ns\n";
std::cout << std::left << std::setw(45) << m_name << " : " << std::right
<< std::setw(12) << dur.count() << " ns\n";
}

private:
Expand Down Expand Up @@ -201,7 +203,7 @@ void test_from_json(std::string filename, auto &obj, const auto &json_str,
const int size) {
iguana::from_json(obj, std::begin(json_str), std::end(json_str));

std::string iguana_str = "iguana parse " + filename;
std::string iguana_str = "iguana from_json " + filename;

{
ScopedTimer timer(iguana_str.data());
Expand All @@ -224,6 +226,18 @@ void test_from_json(std::string filename, auto &obj, const auto &json_str,
#endif
}

void test_dom_parse(std::string filename, const auto &json_str,
const int size) {
std::string iguana_str = "iguana parse " + filename;
iguana::jvalue val;
{
ScopedTimer timer(iguana_str.data());
for (int i = 0; i < size; ++i) {
iguana::parse(val, json_str);
}
}
}

void test_to_json() {
int iterations = 100000;
obj_t obj = create_object();
Expand Down Expand Up @@ -274,12 +288,15 @@ void test_parse() {
{"../data/instruments.json", instruments_t{}},
};

iguana::jvalue val;
for (auto &pair : test_map) {
auto content = iguana::json_file_content(pair.first);

std::visit(
[&](auto &&arg) { test_from_json(pair.first, arg, content, 10); },
pair.second);

test_dom_parse(pair.first, content, 10);
}
}

Expand All @@ -299,4 +316,10 @@ int main() {
test_from_json("obj_t", obj, json0, 100000);
std::cout << "====================\n";
}

iguana::jvalue val;
for (int i = 0; i < 10; ++i) {
test_dom_parse("obj_t", json0, 100000);
std::cout << "====================\n";
}
}

0 comments on commit 2ec4d0e

Please sign in to comment.