Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

有篇文章说“在Go1.10以后性能与标准库差异不大,这个第三方库的意义已经不大了”,是真的吗?有大佬说一下吗? #333

Closed
zwl1619 opened this issue Dec 11, 2018 · 8 comments

Comments

@zwl1619
Copy link

zwl1619 commented Dec 11, 2018

这里有篇文章说“在Go1.10以后性能与标准库差异不大,这个第三方库的意义已经不大了”,是真的吗?有大佬说一下吗?
http://vearne.cc/archives/433

@zhuxiujia
Copy link

这里有篇文章说“在Go1.10以后性能与标准库差异不大,这个第三方库的意义已经不大了”,是真的吗?有大佬说一下吗?
http://vearne.cc/archives/433

你看我提的issues,用json-iterator替换标准库的jsonrpc 性能对比就知道了,这个库在go1.10环境下好像比标准库性能差的多了去了。。。。。

@zhuxiujia
Copy link

这里有篇文章说“在Go1.10以后性能与标准库差异不大,这个第三方库的意义已经不大了”,是真的吗?有大佬说一下吗?
http://vearne.cc/archives/433

怕它在生产环境搞事情,还是用回标准库把,标准库牛逼

@chslink
Copy link

chslink commented Feb 22, 2019

那篇文章没有说清楚,我大概看完了json-iterator的实现.主要优化除了文章里面说的缓存序列化过的结构体以外.还有大量的内存对象池化,减少GC损耗.说白了就是要明确定义字段的结构体.
另外那篇文章测试的结构体都是字符串类型.json格式的字符串类型需要考虑转义,相对其他类型没有办法优化处理.
说难听一点,开源库作者没有售后义务,虽然大部分作者都会热心解答一些问题.建议这种问题还是少问,自己业务场景自己决定要不要用

@rfyiamcool
Copy link

能跑就行了,要啥自行车

@imkos
Copy link

imkos commented Apr 23, 2019

此库等1.13正式版 可能 要打包了。没什么任何优势了, 目前在golang 1.12.4

goos: linux
goarch: amd64
BenchmarkDecodeStdStructMedium-4        	  100000	     17576 ns/op	     368 B/op	       8 allocs/op
BenchmarkDecodeJsoniterStructMedium-4   	  300000	      4251 ns/op	     448 B/op	      43 allocs/op
BenchmarkEncodeStdStructMedium-4        	 2000000	       625 ns/op	     224 B/op	       2 allocs/op
BenchmarkEncodeJsoniterStructMedium-4   	 2000000	       661 ns/op	     224 B/op	       2 allocs/op
BenchmarkStructJsoniter-4               	 2000000	       660 ns/op	     296 B/op	       2 allocs/op
BenchmarkStructStd-4                    	 3000000	       547 ns/op	     288 B/op	       1 allocs/op
BenchmarkMapJsoniter-4                  	  100000	     16847 ns/op	     430 B/op	       4 allocs/op
BenchmarkMapStd-4                       	  100000	     17802 ns/op	     902 B/op	      15 allocs/op
PASS
coverage: 0.0% of statements
ok  	_/root/goWork/linux_app	15.200s

@xuyang2
Copy link

xuyang2 commented Jun 6, 2019

即使“性能与标准库差异不大”,这个库还是挺有意义的。
这个库设计得很灵活,什么都可以扩展。

@huajason
Copy link

多看看别人的源码, 多学习下设计思想, 用不用还是看自己

@ivila
Copy link

ivila commented Nov 29, 2019

注意,我没用jsoniter.ConfigCompatibleWithStandardLibrary

其实在golang 12里面,marshal map 的时候还是有性能优势的。

package main

import (
	'encoding/json'
	jsoniter 'github.com/json-iterator/go'
	'testing'
)

type Car struct {
	Name  string
	Name1 string
	Name2 string
	Name3 string
}

var (
	c     = Car{Name1: 'xxxxxxxxxxxxx', Name2: 'xxxxxxxxxxxxxxxxxxxx', Name3: 'xxxxxxxxxxxxxxxxxx', Name: 'buickxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
	mymap = map[string]string{
		'Name1': 'xxxxxxxxxxxxx',
		'Name2': 'xxxxxxxxxxxxxxxxxxxx',
		'Name3': 'xxxxxxxxxxxxxxxxxx',
		'Name':  'buickxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
	}
	mymap2 = map[string]interface{}{
		'Name1': 'xxxxxxxxxxxxx',
		'Name2': 'xxxxxxxxxxxxxxxxxxxx',
		'Name3': 'xxxxxxxxxxxxxxxxxx',
		'Name':  'buickxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
	}
)

func BenchmarkStructJsoniter(b *testing.B) {
	for i := 0; i < b.N; i++ { //use b.N for looping
		jsoniter.Marshal(&c)
	}
}

func BenchmarkStructStd(b *testing.B) {
	c := Car{Name1: 'xxxxxxxxxxxxx', Name2: 'xxxxxxxxxxxxxxxxxxxx', Name3: 'xxxxxxxxxxxxxxxxxx', Name: 'buickxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'}
	for i := 0; i < b.N; i++ { //use b.N for looping
		json.Marshal(&c)
	}
}

func BenchmarkMapJsoniter(b *testing.B) {
	for i := 0; i < b.N; i++ { //use b.N for looping
		jsoniter.Marshal(mymap)
	}
}

func BenchmarkMapStd(b *testing.B) {
	for i := 0; i < b.N; i++ { //use b.N for looping
		json.Marshal(mymap)
	}
}
func BenchmarkMapJsoniter2(b *testing.B) {
	for i := 0; i < b.N; i++ { //use b.N for looping
		jsoniter.Marshal(mymap2)
	}
}

func BenchmarkMapStd2(b *testing.B) {
	for i := 0; i < b.N; i++ { //use b.N for looping
		json.Marshal(mymap2)
	}
}

测试结果: go version go1.12.13 linux/amd64

goos: linux
goarch: amd64
BenchmarkStructJsoniter 	 1000000	      1608 ns/op	     296 B/op	       2 allocs/op
BenchmarkStructStd      	 1000000	      1451 ns/op	     288 B/op	       1 allocs/op
BenchmarkMapJsoniter    	 1000000	      1745 ns/op	     424 B/op	       4 allocs/op
BenchmarkMapStd         	  300000	      4346 ns/op	     896 B/op	      15 allocs/op
BenchmarkMapJsoniter2   	  500000	      2696 ns/op	     424 B/op	       4 allocs/op
BenchmarkMapStd2        	  200000	      5411 ns/op	     896 B/op	      15 allocs/op
PASS
ok  	_/home/马赛克 13.265s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants