jsoniter与原生json对比

jsoniter与原生json对比

之前看到过json-iterator库使用,为何替换掉原生的json呢,看了一下github,如下:

json-iterator is a high-performance 100% compatible drop-in replacement of "encoding/json"

因为其高性能而为广泛使用,今天测试一个场景,假设有个数组["aaaa", "bbb" .....] 里面存储的全是字符串,字符串长度姑且定位10吧,从字符串Unmarshal为slice string后,采用原生json与json-iter的对比,后面称呼json-iter为jsoniter吧。

jsoniter地址如下:

https://github.com/json-iterator/go

1.场景1: 数组长度10

json

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkJson
BenchmarkJson-8      171060       7230 ns/op     1161 B/op       22 allocs/op
PASS

jsoniter

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkJsonIter
BenchmarkJsonIter-8      443746       2921 ns/op     1011 B/op       24 allocs/op
PASS

2.场景1: 数组长度1000

json

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkJson
BenchmarkJson-8        2842     426381 ns/op    88175 B/op     1036 allocs/op
PASS

jsoniter

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkJsonIter
BenchmarkJsonIter-8       10000     122974 ns/op    65640 B/op     1026 allocs/op
PASS

3.场景3: 数组长度100000

json

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkJson
BenchmarkJson-8          26   43026436 ns/op 10597790 B/op   100260 allocs/op

jsoniter

goos: linux
goarch: amd64
cpu: AMD EPYC 7K62 48-Core Processor
BenchmarkJsonIter
BenchmarkJsonIter-8          75   16120198 ns/op 12341738 B/op   100141 allocs/op
PASS

我们可以看到jsoniter确实比json有这不错的性能优势,从ns/op这个数据看到原生jsoniter是3-4倍的jsoniter,但是从allocs/op上来看效果差距不大。所以针对内存开销比较大的场景,还是适合使用缓存解决,当然缓存存在更新、过期的问题,如果数据长久不变,使用缓存将是不错的选择,如果数据变动非常频繁,那么只能动态的来计算。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值