golang json 解析器性能评测

2016-12-07 09:47:57 +08:00
 taowen

全文链接: https://github.com/json-iterator/go-benchmark

目的不是推销 json-iterator 。而是证明 json-iterator 不比其他的库更慢,从而使得大家可以把吐槽点放到其他方面:比如特性是不是齐全, api 是不是友好。重新发明 json 解析器是因为经常需要处理奇怪格式的 json ,而又不想把数据转两遍。市面上没有 api 满足我的需求的,后面我会专门写一篇 api 介绍的文章来演示 json-iterator 的独特性。( https://github.com/json-iterator/go/blob/master/README.md

测试设备

small payload

https://github.com/json-iterator/go-benchmark/blob/master/src/github.com/json-iterator/go-benchmark/benchmark_small_payload_test.go

encoding/json 在 i7-6700K 上性能还不错,但是在缓存小一些的 cpu 上性能要比这慢更多。

medium payload

https://github.com/json-iterator/go-benchmark/blob/master/src/github.com/json-iterator/go-benchmark/benchmark_medium_payload_test.go

json-iterator 的反射 api 也是相当快的。

large payload

https://github.com/json-iterator/go-benchmark/blob/master/src/github.com/json-iterator/go-benchmark/benchmark_large_payload_test.go

jsonparser 在大部分字段不使用的时候,要快那么一丁点。

large file

test file used: https://github.com/json-iterator/test-data/blob/master/large-file.json

jsonparser 等其他一大票 json 解析器都是以 []byte 作为输入的,简直是为跑分而生。关于这一点 jackson 的作者也有吐槽( https://www.infoq.com/news/2014/05/jackson-founder-responds )。而 jsoniter 可以支持 io.Reader 作为输入,对于大文件处理非常友好。

1393 次点击
所在节点    Go 编程语言
1 条回复
ijustdo
2016-12-07 17:31:58 +08:00
好不容易看到个写 go 的 顶一下呢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/325823

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX