关于go json 的性能效率问题

go 标准库的json 解析很慢,要是考虑到其性能,就考虑使用其它推荐

关于go json 的性能效率问题

ffjson

Go更快的JSON序列化

https://github.com/pquerna/ffjson

ffjson 为 Go 结构产生 MarshalJSON 和 UnmarshalJSON 功能。新生成的功能,减轻了进行序列化时对运行反射的依赖,速度比之前更快2至3倍。

megajson

A JSON parser generator for high performance encoding and decoding in Go.

https://github.com/benbjohnson/megajson

针对json 标准库存在的问题做了优化,它是一个代码生成工具, 通过使用 go/parser 和 go/ast 包去产生你自己类型实体的自定义的 序列号和反序列化代码。 这里的序列号和反序列化知道你的数据类型,所以不会用反射。Go 1.2 下, Megajson 比 encoding/json 包有2倍的性能提升。

go-ujson

ultrajson 的go 实现,接口与 go-simplejson 相似

https://github.com/mreiferson/go-ujson

但是它解析的速度远比不上 ujson

测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
go 1.4.2

标准库 loads  121.017531ms
go-ujson loads  63.217203ms
simplejson loads  90.202043ms
标准库 dumps  88.908435ms

python 2.7.3

simplejson
Loads Taken 14.655828476ms
Dumps Taken 28.5069942474ms

ujson
Loads Taken 12.7279758453
Dumps Taken 13.9889717102

json
Loads Taken 36.0250473022
Dumps Taken 23.5030651093

其它序列化格式

1
2
3
4
5
从结果上上来看, MessagePack , gogo/protobuf ,和 flatbuffers 差不多,这三个优秀的库在序列化和反序列化上各有千秋,而且都是跨语言的。
从便利性上来讲,你可以选择 MessagePack 和 gogo/protobuf 都可以,两者都有大厂在用。
flatbuffers 有点反人类,因为它的操作很底层,而且从结果上来看,序列化的性能要差一点。但是它有一个好处,那就是如果你只需要特定的字段,
你无须将所有的字段都反序列化。从结果上看,不反序列化字段每个调用只用了9.54纳秒,这是因为字段只有在被访问的时候才从byte数组转化为相应的类型。
因此在特殊的场景下,它可以提高N被的性能。但是序列化的代码的面相太难看了。

python 和 go

1
2
3
py ujson > go msgpack x 2
py ujson > go json x 5
py json < go json x 2

参考

本文网址: https://pylist.com/topic/92.html 转摘请注明来源

Suggested Topics

go map 的性能问题

Map 是 go 语言里最常用的数据结构之一,想必都很关注它的性能。下面是一些关于go map 的性能问题的经验,仅供参考...

Leave a Comment