golang json 解析器哪家强?

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

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

  • jsonparser: https://github.com/buger/jsonparser

  • jsoniter pull-api: https://github.com/json-iterator/go

  • jsoniter reflect-api: https://github.com/json-iterator/go/blob/master/jsoniter_reflect.go

  • encoding/json: golang standard lib

  • easy json: https://github.com/mailru/easyjson

测试设备

  • CPU: i7-6700K @ 4.0G

  • Level 1 cache size: 4 x 32 KB 8-way set associative instruction caches

  • Level 2 cache size: 4 x 256 KB 4-way set associative caches

  • Level 3 cache size: 8 MB 16-way set associative shared cache

  • Go: 1.8beta1

small payload

https://github.com/json-itera…

jsonparserjsoniter pull-apijsoniter reflect-apiencoding/jsoneasyjson
599 ns/op515 ns/op684 ns/op2453 ns/op687 ns/op
64 B/op64 B/op256 B/op864 B/op64 B/op
2 allocs/op2 allocs/op4 allocs/op31 allocs/op2 allocs/op

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

《golang json 解析器哪家强?》

medium payload

jsonparserjsoniter pull-apijsoniter reflect-apiencoding/jsoneasyjson
5238 ns/op4111 ns/op4708 ns/op24939 ns/op7361 ns/op
104 B/op104 B/op368 B/op808 B/op248 B/op
4 allocs/op4 allocs/op14 allocs/op18 allocs/op8 allocs/op

https://github.com/json-itera…

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

《golang json 解析器哪家强?》

large payload

https://github.com/json-itera…

jsonparserjsoniter pull-apiencoding/json
38334 ns/op38463 ns/op290778 ns/op
0 B/op0 B/op2128 B/op
0 allocs/op0 allocs/op46 allocs/op

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

《golang json 解析器哪家强?》

large file

test file used: https://github.com/json-itera…

jsonparserjsoniter pull-apiencoding/json
42698634 ns/op37760014 ns/op235354502 ns/op
67107104 B/op4248 B/op71467896 B/op
19 allocs/op5 allocs/op272477 allocs/op

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

《golang json 解析器哪家强?》

    原文作者:taowen
    原文地址: https://segmentfault.com/a/1190000007721025
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞