Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nlohmann VS JsonCpp by C++17 #1633

Closed
zhongcy opened this issue Jun 11, 2019 · 8 comments
Closed

nlohmann VS JsonCpp by C++17 #1633

zhongcy opened this issue Jun 11, 2019 · 8 comments

Comments

@zhongcy
Copy link

zhongcy commented Jun 11, 2019

Hi, I test nlohmann and JsonCpp in C++17.

Env:
CentOS 7.5 64Bit
4vCPU, 8GB Mem, 100GB Disk

nlohmann Release 3.6.1
jsoncpp Release 0.10.5

[root@zhongcy home]$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-7/root/usr/libexec/gcc/x86_64-redhat-linux/7/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-7/root/usr --mandir=/opt/rh/devtoolset-7/root/usr/share/man --infodir=/opt/rh/devtoolset-7/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --enable-plugin --with-linker-hash-style=gnu --enable-initfini-array --with-default-libstdcxx-abi=gcc4-compatible --with-isl=/builddir/build/BUILD/gcc-7.3.1-20180303/obj-x86_64-redhat-linux/isl-install --enable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC)

Codes and result like:

`
#include "nlohmann/json.hpp"
#include <json/writer.h>

std::string sStr =
R"json(
{ "name": "myserver-consumer#consumer-1", "client_id": "myserver-consumer", "type": "consumer", "ts":1916750966985, "time":1560232284, "replyq":0, "msg_cnt":0, "msg_size":0, "msg_max":0, "msg_size_max":0, "simple_cnt":0, "metadata_cache_cnt":1, "brokers":{ "127.0.0.1:9092/1": { "name":"127.0.0.1:9092/1", "nodeid":1, "nodename":"127.0.0.1:9092", "source":"learned", "state":"INIT", "stateage":1989943, "outbuf_cnt":0, "outbuf_msg_cnt":0, "waitresp_cnt":0, "waitresp_msg_cnt":0, "tx":0, "txbytes":0, "txerrs":0, "txretries":0, "req_timeouts":0, "rx":0, "rxbytes":0, "rxerrs":0, "rxcorriderrs":0, "rxpartial":0, "zbuf_grow":0, "buf_grow":0, "wakeups":0, "connects":0, "disconnects":0, "int_latency": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 11376, "cnt":0 }, "outbuf_latency":{ "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 11376, "cnt":0 }, "rtt": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 13424, "cnt":0 }, "throttle": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0,"outofrange": 0, "hdrsize": 17520, "cnt":0 }, "req": { "Fetch": 0, "Offset": 0, "Metadata": 0, "OffsetCommit": 0, "OffsetFetch": 0, "GroupCoordinator": 0, "JoinGroup": 0, "Heartbeat": 0, "LeaveGroup": 0, "SyncGroup": 0, "SaslHandshake": 0, "ApiVersion": 0 }, "toppars":{ } } , "127.0.0.1:9092/bootstrap": { "name":"127.0.0.1:9092/bootstrap", "nodeid":-1, "nodename":"127.0.0.1:9092", "source":"configured", "state":"UP", "stateage":1993368, "outbuf_cnt":0, "outbuf_msg_cnt":0, "waitresp_cnt":0, "waitresp_msg_cnt":0, "tx":5, "txbytes":242, "txerrs":0, "txretries":0, "req_timeouts":0, "rx":5, "rxbytes":577, "rxerrs":0, "rxcorriderrs":0, "rxpartial":0, "zbuf_grow":0, "buf_grow":0, "wakeups":15, "connects":1, "disconnects":0, "int_latency": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 11376, "cnt":0 }, "outbuf_latency": { "min":32, "max":342, "avg":144, "sum":433, "stddev": 140, "p50": 59, "p75": 59, "p90": 343, "p95": 343, "p99":343, "p99_99": 343, "outofrange": 0, "hdrsize": 11376, "cnt":3 }, "rtt": { "min":3173, "max":4331, "avg":3690, "sum":11070, "stddev": 482, "p50": 3567, "p75": 3567, "p90": 4351, "p95": 4351, "p99": 4351, "p99_99": 4351, "outofrange": 0, "hdrsize": 13424, "cnt":3 }, "throttle": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 17520, "cnt":0 }, "req": { "Fetch": 0, "Offset": 0, "Metadata": 2, "OffsetCommit": 0, "OffsetFetch": 0, "GroupCoordinator": 2, "JoinGroup": 0, "Heartbeat": 0, "LeaveGroup": 0, "SyncGroup": 0, "SaslHandshake": 0, "ApiVersion": 1 }, "toppars":{ } } , "GroupCoordinator": { "name":"GroupCoordinator", "nodeid":-1, "nodename":"127.0.0.1:9092", "source":"logical", "state":"UP", "stateage":990212, "outbuf_cnt":0, "outbuf_msg_cnt":0, "waitresp_cnt":0, "waitresp_msg_cnt":0, "tx":2, "txbytes":80, "txerrs":0, "txretries":0, "req_timeouts":0, "rx":2, "rxbytes":353, "rxerrs":0, "rxcorriderrs":0, "rxpartial":0, "zbuf_grow":0, "buf_grow":0, "wakeups":6, "connects":1, "disconnects":0, "int_latency": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 11376, "cnt":0 }, "outbuf_latency": { "min":40, "max":49, "avg":44, "sum":89, "stddev": 4, "p50": 40, "p75": 49, "p90": 49, "p95": 49, "p99": 49, "p99_99": 49, "outofrange": 0, "hdrsize": 11376, "cnt":2 }, "rtt": { "min":2757, "max":2835, "avg":2796, "sum":5592, "stddev": 40, "p50": 2767, "p75": 2847, "p90": 2847, "p95": 2847, "p99": 2847, "p99_99": 2847, "outofrange": 0, "hdrsize": 13424, "cnt":2 }, "throttle": { "min":0, "max":0, "avg":0, "sum":0, "stddev": 0, "p50": 0, "p75": 0, "p90": 0, "p95": 0, "p99": 0, "p99_99": 0, "outofrange": 0, "hdrsize": 17520, "cnt":0 }, "req": { "Fetch": 0, "Offset": 0, "Metadata": 1, "OffsetCommit": 0, "OffsetFetch": 0, "GroupCoordinator": 0, "JoinGroup": 0, "Heartbeat": 0, "LeaveGroup": 0, "SyncGroup": 0, "SaslHandshake": 0, "ApiVersion": 1 }, "toppars":{ } } }, "topics":{ } , "cgrp": { "state": "up", "stateage": 987, "join_state": "init", "rebalance_age": 983, "rebalance_cnt": 2, "rebalance_reason": "group rejoin", "assignment_size": 0 }, "tx":7, "tx_bytes":322, "rx":7, "rx_bytes":930, "txmsgs":0, "txmsg_bytes":0, "rxmsgs":0, "rxmsg_bytes":0}
)json";

uint64_t timebegin = now();
if (false)
{
nlohmann::json sRoot = nlohmann::json::parse(sStr);
} else {
//JsonCpp
Json::Reader sReader;
Json::Value sRoot;
bool sRet = sReader.parse(sStr, sRoot);
}
uint64_t timeused = now() - timebegin;
LOG_DEBUG("timeused:%ld", timeused);
`

Use default: -O0

resule:
JsonCpp: timeused:513
nlohmann: timeused:5345

Why nlohmann::parse is such slower than JsonCpp::parse ?
Is nlohmann not high performance by C++17 ?
@nlohmann @theodelrieu

@zhongcy zhongcy closed this as completed Jun 11, 2019
@zhongcy zhongcy reopened this Jun 11, 2019
@zhongcy zhongcy closed this as completed Jun 11, 2019
@zhongcy
Copy link
Author

zhongcy commented Jun 11, 2019

Ah~, jsoncpp is relase lib. jsoncpp lib use -O2/-O3, so jsoncpp lib is better than default nlohmann -O0.

@hydratim
Copy link

How was the comparison when you compiled everything with -O2?

@zhongcy
Copy link
Author

zhongcy commented Jun 11, 2019

@hydratim If we use liba.so in our code, we should compare the .so by -O2, I think.

@hydratim
Copy link

Sorry, I wasn't clear; What I meant was what did the performance of nlohmann::json look like compared to jsoncpp when you corrected your compilation difference?

@zhongcy
Copy link
Author

zhongcy commented Jun 12, 2019

@hydratim When I use -O2 to compare nlohmann::json, the performance is better.
But the nlohmann::json's performance is still lower than jsoncpp's in my test.

@hydratim
Copy link

Thanks

@laoshaw
Copy link

laoshaw commented Aug 6, 2020

I just did a test myself it seems nlohmann json is actually slightly faster? I basically did the parse 10,000 loops and nolhmann takes 1092805microconds while jsoncpp takes 1368531microseconds, using c++17, use hlohmann as head file directly, while jsoncpp as a library linked in. static build give similar results(1015422microconds vs 1130625microseconds)

@nlohmann
Copy link
Owner

nlohmann commented Aug 7, 2020

These benchmarks seem to reproduce this finding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants