作者:Milo Yip
链接:http://www.zhihu.com/question/23654513/answer/46157616
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
链接:http://www.zhihu.com/question/23654513/answer/46157616
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
身为 RapidJSON 作者,刚刚做完 28 个 C/C++ JSON 库(包括题目中的两个)的评测
miloyip/nativejson-benchmark,回来回答这个问题。
标准符合程度(越高越好):
<img src="https://pic3.zhimg.com/0824ee0557ff57fbcdbc98b82477f30a_b.jpg" data-rawwidth="900" data-rawheight="810" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic3.zhimg.com/0824ee0557ff57fbcdbc98b82477f30a_r.jpg">这个测试分开4个部分:JSON正确性检测(使用 这个测试分开4个部分:JSON正确性检测(使用 JSON_checker测试集)、解析double的准确性、解析string的正确性、roundtrip测试。
JsonCpp 得88分,算是比较好的了。如果仔细看每个部分( https://rawgit.com/miloyip/nativejson-benchmark/master/sample/conformance.html),就是JSON正确性和roundtrip上有几个问题。而RapidJSON在JSON正确性、string和roundtrip上都是满分,开启了 full precision 选项的话,解析double也是满分。
解析和生成JSON的耗时(越低越好):
<img src="https://pic2.zhimg.com/d995b8cfc3249d112fa9142e3d5e7101_b.jpg" data-rawwidth="900" data-rawheight="833" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic2.zhimg.com/d995b8cfc3249d112fa9142e3d5e7101_r.jpg"> <img src="https://pic4.zhimg.com/70e06ed8e2625bc4d4ba4aec1fda1973_b.jpg" data-rawwidth="900" data-rawheight="765" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic4.zhimg.com/70e06ed8e2625bc4d4ba4aec1fda1973_r.jpg">可见RapidJSON比JsonCpp在解析上快了一个数量级,而生成上也近一个数量级了。 可见RapidJSON比JsonCpp在解析上快了一个数量级,而生成上也近一个数量级了。
解析至DOM后的内存用量(越低越好):
<img src="https://pic4.zhimg.com/9b0bdc335161513ab69b7b43de24c6cb_b.jpg" data-rawwidth="900" data-rawheight="833" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic4.zhimg.com/9b0bdc335161513ab69b7b43de24c6cb_r.jpg">RapidJSON(除了insitu parsing)的内存用量只是JsonCpp的1/4。 RapidJSON(除了insitu parsing)的内存用量只是JsonCpp的1/4。
其他方面,例如功能、文档、友好性等,就不在此逐一对比了。
RapidJSON也有一些缺点,例如有些API的设计比较奇怪,可能较难使用。我们会尝试加入一些功能,使不太注重性能的部分能更容易使用。
如果读者仍然想使用 JsonCpp,也可以参考这个评测,帮助改进它。这也是我做此评测的初衷,在开源代码中互相学习,持续改进。
标准符合程度(越高越好):
<img src="https://pic3.zhimg.com/0824ee0557ff57fbcdbc98b82477f30a_b.jpg" data-rawwidth="900" data-rawheight="810" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic3.zhimg.com/0824ee0557ff57fbcdbc98b82477f30a_r.jpg">这个测试分开4个部分:JSON正确性检测(使用 这个测试分开4个部分:JSON正确性检测(使用 JSON_checker测试集)、解析double的准确性、解析string的正确性、roundtrip测试。
JsonCpp 得88分,算是比较好的了。如果仔细看每个部分( https://rawgit.com/miloyip/nativejson-benchmark/master/sample/conformance.html),就是JSON正确性和roundtrip上有几个问题。而RapidJSON在JSON正确性、string和roundtrip上都是满分,开启了 full precision 选项的话,解析double也是满分。
解析和生成JSON的耗时(越低越好):
<img src="https://pic2.zhimg.com/d995b8cfc3249d112fa9142e3d5e7101_b.jpg" data-rawwidth="900" data-rawheight="833" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic2.zhimg.com/d995b8cfc3249d112fa9142e3d5e7101_r.jpg"> <img src="https://pic4.zhimg.com/70e06ed8e2625bc4d4ba4aec1fda1973_b.jpg" data-rawwidth="900" data-rawheight="765" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic4.zhimg.com/70e06ed8e2625bc4d4ba4aec1fda1973_r.jpg">可见RapidJSON比JsonCpp在解析上快了一个数量级,而生成上也近一个数量级了。 可见RapidJSON比JsonCpp在解析上快了一个数量级,而生成上也近一个数量级了。
解析至DOM后的内存用量(越低越好):
<img src="https://pic4.zhimg.com/9b0bdc335161513ab69b7b43de24c6cb_b.jpg" data-rawwidth="900" data-rawheight="833" class="origin_image zh-lightBox-thumb" width="900" data-original="https://pic4.zhimg.com/9b0bdc335161513ab69b7b43de24c6cb_r.jpg">RapidJSON(除了insitu parsing)的内存用量只是JsonCpp的1/4。 RapidJSON(除了insitu parsing)的内存用量只是JsonCpp的1/4。
其他方面,例如功能、文档、友好性等,就不在此逐一对比了。
RapidJSON也有一些缺点,例如有些API的设计比较奇怪,可能较难使用。我们会尝试加入一些功能,使不太注重性能的部分能更容易使用。
如果读者仍然想使用 JsonCpp,也可以参考这个评测,帮助改进它。这也是我做此评测的初衷,在开源代码中互相学习,持续改进。