Re-test Golang JSON class library

Source: Internet
Author: User

Write the project always need to be serialized, heard, also saw a lot of classmates teacher to each Golang jsonLibrary for evaluation. Why do I have to continue this assessment?
Because the practice of knowledge is the most persuasive, but also belongs to their own, I also hope to see Ben Boven classmate Teacher can modify and perform the evaluation of code execution once, I believe there will not necessarily experience.
This test I chose the Class library has:

Class Library
Serial Number Class Library Address Notes
1 Encoding/json Golan
2 Easyjson Github.com/mailru/easyjson
3 Ffjson Github.com/mailru/easyjson
4 Iterator/json Github.com/json-iterator/go

Mainly for the above-mentioned types, I used a different class library using different structures (only the struct name is different, field order and type).

Environment

Environment for MacBook Pro (Core i5 processor/8GB memory) go1.8.3 DARWIN/AMD64

Code

The bench code is as follows:

Package Jsonbenchimport ("Encoding/gob" "Encoding/json" "Github.com/json-iterator/go" "Github.com/mailru/easy JSON "" Github.com/pquerna/ffjson/ffjson "" Testing ") var (iterator = Jsoniter.        Configcompatiblewithstandardlibrary//Easyjson as = agentservice{ServiceName: "Kaleidoscope_api", Version: "1517558949087295000_1298498081", ServiceId: "kaleidoscope_kaleidoscope.dev.igetget.com_v1.2" , Address: "kaleidoscope.dev.igetget.com", port:80, Metadata:map[string]stri ng{}, connecttimeout:1000, Connecttype: "LONG", readtimeout:1000, writetimeout:1000      , Protocol: "HTTP", Balance: "Random", Idcs: "Hu,hd,hn", Converter: "JSON", retry:3,} service = as. Toservice () asbytes, _ = json. Marshal (AS) servicebytes, _ = json.       Marshal (Service) ASSTR       = String (asbytes) Servicestr = string (servicebytes) asgonbytes, _ = Gobencode (AS) servicegonb Ytes, _ = Gobencode (service)/std ASSTD = agentservicestd{ServiceName: "Kaleidoscope_api", Versi        On: "1517558949087295000_1298498081", ServiceId: "kaleidoscope_kaleidoscope.dev.igetget.com_v1.2",        Address: "Kaleidoscope.dev.igetget.com", Port:80, metadata:map[string]string{},        connecttimeout:1000, Connecttype: "LONG", readtimeout:1000, writetimeout:1000, Protocol: "HTTP", Balance: "Random", Idcs: "Hu,hd,hn", Converter: "Jso N ", Retry:3,} servicestd = Asstd. TOSERVICESTD () asbytesstd, _ = json. Marshal (ASSTD) servicebytesstd, _ = json. Marshal (servicestd) asstrstd = string (asbytesstd) servicestrstd = string (servicEBYTESSTD) Asgonbytesstd, _ = Gobencode (ASSTD) servicegonbytesstd, _ = Gobencode (SERVICESTD))//GO test-bench= ". *" Func init () {gob. Register (agentservice{})}func Benchmark_std_marshal1 (b *testing. B) {for i: = 0; i < b.n*10; i++ {_, Err: = json. Marshal (ASSTD) if err! = Nil {b.error (err)}}}func Benchmark_std_marshal2 (b *testing. B) {for i: = 0; i < b.n*10; i++ {_, Err: = json. Marshal (SERVICESTD) if err! = Nil {b.error (err)}}}func Benchmark_easyjson_std_marshal1 (b *t Esting. B) {for i: = 0; i < b.n*10; i++ {_, Err: = json. Marshal (AS) if err! = Nil {b.error (err)}}}func Benchmark_easyjson_std_marshal2 (b *testing.b {for I: = 0; i < b.n*10; i++ {_, Err: = json. Marshal (Service) if err! = Nil {b.error (err)}}}func Benchmark_easyjson_marshal1 (b *testing. B) {for i: = 0; i < b.n*10; i++ {_, ERR: = Easyjson. Marshal (AS) if err! = Nil {b.error (err)}}}func Benchmark_easyjson_marshal2 (b *testing. B) {for i: = 0; i < b.n*10; i++ {_, Err: = Easyjson. Marshal (Service) if err! = Nil {b.error (err)}}}//func Benchmark_iterator_marshal1 (b *testin G.B) {for i: = 0; i < b.n*10; i++ {_, Err: = iterator. Marshal (ASSTD) if err! = Nil {b.error (err)}}}func Benchmark_iterator_marshal2 (b *testing. B) {for i: = 0; i < b.n*10; i++ {_, Err: = iterator. Marshal (SERVICESTD) if err! = Nil {b.error (err)}}}func Benchmark_ffjson_marshal1 (b *testing . B) {for i: = 0; i < b.n*10; i++ {_, Err: = Ffjson. Marshal (ASSTD) if err! = Nil {b.error (err)}}}func Benchmark_ffjson_marshal2 (b *testing. B) {for i: = 0; i < b.n*10; i++ {_, Err: = Ffjson.        Marshal (SERVICESTD) if err! = Nil {    B.error (Err)}}}func benchmark_gob_encode1 (b *testing. B) {for i: = 0; i < b.n*10; i++ {as. Port = i gobencode (AS)}}func Benchmark_gob_encode2 (b *testing. b) {for i: = 0; i < b.n*10; i++ {gobencode (service)}}func Benchmark_std_unmarshal1 (b *testing. B) {tmp: = agentservicestd{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = json. Unmarshal (ASBYTESSTD, &tmp) if err! = Nil {b.error (err)}}}func Benchmark_std_unmarshal2 (b *testing. B) {tmp: = servicestd{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = json. Unmarshal (SERVICEBYTESSTD, &tmp) if err! = Nil {b.error (err)}}}func Benchmark_easyjson_ Std_unmarshal1 (b *testing. B) {tmp: = agentservice{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = json. Unmarshal (Asbytes, &tmp) if err! = Nil {b.error (err)}}}func BencHmark_easyjson_std_unmarshal2 (b *testing. B) {tmp: = service{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = json. Unmarshal (Servicebytes, &tmp) if err! = Nil {b.error (err)}}}func BENCHMARK_EASYJSON_UNM Arshal1 (b *testing. B) {tmp: = agentservice{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = Easyjson. Unmarshal (Asbytes, &tmp) if err! = Nil {b.error (err)}}}func Benchmark_easyjson_unmarsha L2 (b *testing. B) {tmp: = service{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = Easyjson. Unmarshal (Servicebytes, &tmp) if err! = Nil {b.error (err)}}}func BENCHMARK_ITERATOR_UNM Arshal1 (b *testing. B) {tmp: = servicestd{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = iterator. Unmarshal (SERVICEBYTESSTD, &tmp) if err! = Nil {b.error (err)}}}func benchmark_iterator_ UnMArshal2 (b *testing. B) {tmp: = servicestd{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = iterator. Unmarshal (SERVICEBYTESSTD, &tmp) if err! = Nil {b.error (err)}}}func Benchmark_ffjson_un Marshal1 (b *testing. B) {tmp: = servicestd{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = Ffjson. Unmarshal (SERVICEBYTESSTD, &tmp) if err! = Nil {b.error (err)}}}func Benchmark_ffjson_un Marshal2 (b *testing. B) {tmp: = servicestd{} for I: = 0; i < b.n*10; i++ {as. Port = I err: = Ffjson. Unmarshal (SERVICEBYTESSTD, &tmp) if err! = Nil {b.error (err)}}}func benchmark_gob_decod E1 (b *testing. B) {tmp: = agentservice{} for I: = 0; i < b.n*10; i++ {as. Port = i gobdecode (asgonbytes, &tmp)}}func Benchmark_gob_decode2 (b *testing. B) {tmp: = service{} for I: = 0; i < b.n*10; i++ {as. Port = i       Gobdecode (Servicegonbytes, &tmp)}} 

Execute command:

go test -bench=".*"

Assessment results;

$ go test-bench= ". *" Benchmark_std_marshal1-4 50000 31224 ns/opbenchmark_std_marshal2-4 30000 49598 ns/opbenchmark_easyjson_std_marshal1-4 30000 45778 Ns/opbenchma             Rk_easyjson_std_marshal2-4 30000 50440 ns/opbenchmark_easyjson_marshal1-4 100000             14387 ns/opbenchmark_easyjson_marshal2-4 100000 16009 ns/opbenchmark_iterator_marshal1-4 100000 14899 ns/opbenchmark_iterator_marshal2-4 100000 21629 ns/opbenchmark_ffj Son_marshal1-4 50000 31633 ns/opbenchmark_ffjson_marshal2-4 30000 51                    668 ns/opbenchmark_gob_encode1-4 20000 97099 ns/opbenchmark_gob_encode2-4 10000 153158 ns/opbenchmark_std_unmarshal1-4 20000 89211 Ns/opbenchmark_std_unmar                 Shal2-420000 76442 ns/opbenchmark_easyjson_std_unmarshal1-4 30000 57695 Ns/opbenchmark_easyjson_st D_unmarshal2-4 20000 66269 ns/opbenchmark_easyjson_unmarshal1-4 100000 19028 ns/o             Pbenchmark_easyjson_unmarshal2-4 100000 22035 ns/opbenchmark_iterator_unmarshal1-4 50000 35942 ns/opbenchmark_iterator_unmarshal2-4 50000 36462 ns/opbenchmark_ffjson_unmarshal1 -4 20000 80290 ns/opbenchmark_ffjson_unmarshal2-4 20000 78431 ns/opbench            Mark_gob_decode1-4 377698 ns/opbenchmark_gob_decode2-4 3000 463472 Ns/oppassok Studygo/jsonbench 49.174s
Conclusion
    1. Which class library is the fastest?
      A: The fastest in the Evaluation class library. Speed: Easyjson = iterator = Encoding/json = Ffjson
    2. Is there a pit?
      A: easyjson There is a pit that can be seen from the code Benchmark_EASYJSON_STD_* , because the Easyjson generated code already contains the MarshalJSON and UnmarshalJSON methods, so long as the execution of these structures json.marshalJSON and json.UnmarshalJSON will default to call the Easyjson generated method. I run it many times, and I find that the method called Easyjson generated is about MarshalJSON 50% slower than the standard library, but the call easyjson generated is UnmarshalJSON about 20% faster than the standard library.
    3. How to choose?
      Answer: easyjson Although the speed is relatively fast, but there are some unsuitable scenarios, such as if the interface interface needs to be serialized. It is recommended easyjson to use a combination with the standard library.

Re-test Golang JSON class library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.