最快的 C++ JSON 读写器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3512650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest JSON reader/writer for C++
提问by user424060
I need a C++ JSON parser & writer. Speed and reliability are very critical, I don't care if the interface is nice or not, if it's Boost-based or not, even a C parser is fine (if it's considerably faster than C++ ones).
我需要一个 C++ JSON 解析器和编写器。速度和可靠性非常关键,我不在乎界面是否好,是否基于 Boost,即使是 C 解析器也很好(如果它比 C++ 快得多)。
If somebody has experience with the speed of available JSON parsers, please advise.
如果有人对可用 JSON 解析器的速度有经验,请提供建议。
采纳答案by Spudd86
Don't really know how they compare for speed, but the first one looks like the right idea for scaling to really big JSON data, since it parses only a small chunk at a time so they don't need to hold all the data in memory at once (This can be faster or slower depending on the library/use case)
真的不知道它们的速度比较如何,但第一个看起来是扩展到非常大的 JSON 数据的正确想法,因为它一次只解析一小块,所以他们不需要保存所有数据一次内存(这可能更快或更慢,具体取决于库/用例)
回答by Milo Yip
rapidjsonis a C++ JSON parser/generator designed to be fast and small memory footprint.
Rapidjson是一个 C++ JSON 解析器/生成器,旨在实现快速和小内存占用。
There is a performance comparisonwith YAJL and JsonCPP.
有与 YAJL 和 JsonCPP的性能比较。
Update:
更新:
I created an open source project Native JSON benchmark, which evaluates 29 (and increasing) C/C++ JSON libraries, in terms of conformance and performance. This should be an useful reference.
我创建了一个开源项目Native JSON benchmark,它在一致性和性能方面评估了 29 个(并且还在增加)C/C++ JSON 库。这应该是一个有用的参考。
回答by kert
https://github.com/quartzjer/js0n
https://github.com/quartzjer/js0n
Ugliest interface possible, but does what you ask. Zero allocations.
可能是最丑陋的界面,但会按您的要求执行。零分配。
http://zserge.com/jsmn.htmlAnother zero-allocation approach.
http://zserge.com/jsmn.html另一种零分配方法。
The solutions posted above all do dynamic memory allocation, hence will be inevitably end up slower at some point, depending on the data structure - and will be dangerous to include in a heap constrained environment like an embedded system.
上面发布的解决方案都进行动态内存分配,因此在某些时候不可避免地会变慢,具体取决于数据结构 - 并且将其包含在像嵌入式系统这样的堆受限环境中是危险的。
Benchmarks of vjson, rapidjson and sajson here : http://chadaustin.me/2013/01/json-parser-benchmarking/if you are interested in that sort of thing.
vjson、rapidjson 和 sajson 的基准测试在这里:http://chadaustin.me/2013/01/json-parser-benchmarking/如果您对这类事情感兴趣。
And to answer your "writer" part of the question i doubt that you could beat an efficient
为了回答问题的“作家”部分,我怀疑您能否击败高效的
printf("{%s:%s}",name,value)
implementation with any library - assuming your printf/sprintf implementation itself is lightweight of course.
使用任何库实现——当然,假设你的 printf/sprintf 实现本身是轻量级的。
EDIT: actually let me take that back, RapidJson allows on-stack allocation only through its MemoryPoolAllocator and actually makes this a default for its GenericReader. I havent done the comparison but i would expect it to be more robust than anything else listed here. It also doesnt have any dependencies, and it doesnt throw exceptions which probably makes it ultimately suitable for embedded. Fully header based lib so, easy to include anywhere.
编辑:实际上让我收回这一点,RapidJson 仅允许通过其 MemoryPoolAllocator 进行堆栈分配,并且实际上使其成为其 GenericReader 的默认值。我还没有做过比较,但我希望它比这里列出的任何其他东西都更强大。它也没有任何依赖项,并且不会抛出异常,这可能使其最终适合嵌入式。完全基于头文件的库,所以很容易包含在任何地方。