鸿 记5d13.cn

a programer log.

Home golang work

golang标准包gob比json的性能低一半?

golang标准包gob比json的性能低一半?

By 鸿记

在游戏服务器的开发过程中肯定需要用到数据序列化,本文对golang标准库的gob和json两种编码库进行了性能测试出现意料之外的事情:json的性能和字节效率都比gob高,why?

前言

在游戏服务器的开发过程中肯定需要用到数据序列化,于是我对golang标准库的gob和json两种编码方式进行了性能测试。结果却跌破我的眼睛:json的性能和字节效率都比gob高,这否定了我之前的“常识”——几乎所有介绍gob的文章都说gob的编码效率比json高。于是我将测试代码及环境记录下来,希望有兴趣的朋友共同探讨。

测试机配置

我的测试机(也是我的开发机器)是mac mini,具体配置如下:

2.3GHz Mac mini 2.3GHz 四核 Intel Core i7 处理器 4GB 内存 1TB 硬盘1 Intel HD Graphics 4000 图形处理器 OS X Mountain Lion

测试代码

完整代码可以在github.com 上的 letsgo 项目下载

serialize.go

getsession() ptr <nil>
getsession() Invalid container type invalid

serialize_b_test.go

package helper

import ( "testing" "fmt" )

func Benchmark_SerializeGob(t *testing.B) { //fmt.Println("////////////////////////test serialize b //////////////////////////////") for i := 0; i < t.N; i++ { test() } bytelength := test() fmt.Println("bytelengthGob=",bytelength) }

func test() int { bl := 0 gs := &LGGobSerialize{}

</span><span class="n">v1</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="m">23422</span><span class="x">
</span><span class="n">v2</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="o">-</span><span class="m">1</span><span class="x">

</span><span class="n">v</span><span class="p">,</span><span class="n">_</span><span class="o">:=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Serialize</span><span class="p">(</span><span class="n">v1</span><span class="p">)</span><span class="x">
</span><span class="c">//bl += len(v)</span><span class="x">
</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Deserialize</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="o">&amp;</span><span class="n">v2</span><span class="p">)</span><span class="x">

</span><span class="n">v1</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="m">0</span><span class="x">
</span><span class="n">v2</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="o">-</span><span class="m">2</span><span class="x">
</span><span class="n">v</span><span class="p">,</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Serialize</span><span class="p">(</span><span class="n">v1</span><span class="p">)</span><span class="x">
</span><span class="c">//bl += len(v)</span><span class="x">
</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Deserialize</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="o">&amp;</span><span class="n">v2</span><span class="p">)</span><span class="x">

</span><span class="n">a1</span><span class="p">,</span><span class="n">_</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="n">NewA1A2</span><span class="p">()</span><span class="x">

</span><span class="n">a2</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="o">&amp;</span><span class="n">A</span><span class="p">{}</span><span class="x">
</span><span class="n">v</span><span class="p">,</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Serialize</span><span class="p">(</span><span class="n">a1</span><span class="p">)</span><span class="x">
</span><span class="n">bl</span><span class="x"> </span><span class="o">+=</span><span class="x"> </span><span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="x">
</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Deserialize</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="o">&amp;</span><span class="n">a2</span><span class="p">)</span><span class="x">

</span><span class="k">return</span><span class="x"> </span><span class="n">bl</span><span class="x">

}

func Benchmark_Serialize_Json(t *testing.B) { //fmt.Println("////////////////////////test serialize b //////////////////////////////") for i := 0; i < t.N; i++ { test2() }

</span><span class="n">bytelength</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="n">test2</span><span class="p">()</span><span class="x">
</span><span class="n">fmt</span><span class="o">.</span><span class="n">Println</span><span class="p">(</span><span class="s">"bytelengthJson="</span><span class="p">,</span><span class="n">bytelength</span><span class="p">)</span><span class="x">

}

func test2() int { bl := 0 gs := &LGJsonSerialize{}

</span><span class="n">v1</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="m">23422</span><span class="x">
</span><span class="n">v2</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="o">-</span><span class="m">1</span><span class="x">

</span><span class="n">v</span><span class="p">,</span><span class="n">_</span><span class="o">:=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Serialize</span><span class="p">(</span><span class="n">v1</span><span class="p">)</span><span class="x">
</span><span class="c">//bl += len(v)</span><span class="x">
</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Deserialize</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="o">&amp;</span><span class="n">v2</span><span class="p">)</span><span class="x">

</span><span class="n">v1</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="m">0</span><span class="x">
</span><span class="n">v2</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="o">-</span><span class="m">2</span><span class="x">
</span><span class="n">v</span><span class="p">,</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Serialize</span><span class="p">(</span><span class="n">v1</span><span class="p">)</span><span class="x">
</span><span class="c">//bl += len(v)</span><span class="x">
</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Deserialize</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="o">&amp;</span><span class="n">v2</span><span class="p">)</span><span class="x">

</span><span class="n">a1</span><span class="p">,</span><span class="n">_</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="n">NewA1A2</span><span class="p">()</span><span class="x">

</span><span class="n">a2</span><span class="x"> </span><span class="o">:=</span><span class="x"> </span><span class="o">&amp;</span><span class="n">A</span><span class="p">{}</span><span class="x">
</span><span class="n">v</span><span class="p">,</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Serialize</span><span class="p">(</span><span class="n">a1</span><span class="p">)</span><span class="x">
</span><span class="n">bl</span><span class="x"> </span><span class="o">+=</span><span class="x"> </span><span class="nb">len</span><span class="p">(</span><span class="n">v</span><span class="p">)</span><span class="x">
</span><span class="n">_</span><span class="x"> </span><span class="o">=</span><span class="x"> </span><span class="n">gs</span><span class="o">.</span><span class="n">Deserialize</span><span class="p">(</span><span class="n">v</span><span class="p">,</span><span class="o">&amp;</span><span class="n">a2</span><span class="p">)</span><span class="x">

</span><span class="k">return</span><span class="x"> </span><span class="n">bl</span><span class="x">

}

完整代码可以在github.com 上的 letsgo 项目下载

测试结果

$ go test -bench=".*" Benchmark_SerializeGob bytelengthGob= 507 10000 169793 ns/op Benchmark_Serialize_Json bytelengthJson= 470 20000 89994 ns/op ok github.com/sunminghong/letsgo/helper 4.446s

结论&问题

  1. 显而易见,json要快一倍,且编码字节数略省(8%左右)
  2. 经反复测试,gob方式可对任何数据类型编码、解码;json 对map类型编码,key值必须是字符串类型

数据说明一切,但是我还是希望有兴趣的朋友一起研究下。

孙铭鸿logo
by 鸿记

声明:本站文章除注明转载外,均为本站原创或者翻译。
本文采用 署名-非商业性使用-相同方式共享(BY-NC-SA) 协议进行授权。
转载请注明转自: 【鸿记】golang标准包gob比json的性能低一半?

Fork me on GitHub