GolangNote

Golang笔记

关于Golang msgpack 和 json 性能的简单比较

Permalink

一个关于Golang msgpack 和 json 性能的简单比较:

Go: msgpack 和 json 性能比较
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
    "fmt"
    "time"

    "encoding/json"
    "gopkg.in/vmihailenco/msgpack.v2"
)

type ts struct {
    C   string
    K   string
    T   int
    Max int
    Cn  string
}

func main() {

    var in = &ts{
        C:   "LOCK",
        K:   "31uEbMgunupShBVTewXjtqbBv5MndwfXhb",
        T:   1000,
        Max: 200,
        Cn:  "中文",
    }

    ExampleMsgpack(in)
    ExampleJson(in)
}

func ExampleJson(in *ts) {

    t1 := time.Now()

    for i := 0; i < 100000; i++ {
        // encode
        b, _ := json.Marshal(in)
        // decode
        var out = ts{}
        _ = json.Unmarshal(b, &out)
    }
    t2 := time.Now()
    fmt.Println("Json 消耗时间:", t2.Sub(t1), "秒")
}

func ExampleMsgpack(in *ts) {

    t1 := time.Now()

    for i := 0; i < 100000; i++ {
        // encode
        b, _ := msgpack.Marshal(in)
        // decode
        var out = ts{}
        _ = msgpack.Unmarshal(b, &out)
    }
    t2 := time.Now()
    fmt.Println("msgpack 消耗时间:", t2.Sub(t1), "秒")
}

输出:

plaintext: 性能测试结果
1
2
msgpack 消耗时间: 409.721398ms 秒
Json 消耗时间: 446.545891ms 秒

相差不大,把 string 字段加长

Go: 把 string 字段加长
1
2
3
4
5
6
7
var in = &ts{
    C:   "LOCK美国宇航局退休专家弗里德曼-斯科特在自传中披露",
    K:   "31uEbMgunupShBVTewXjtqbBv5MndwfXhb美国宇航局退休专家弗里德曼-斯科特在自传中披露",
    T:   1000,
    Max: 200,
    Cn:  "中文美国宇航局退休专家弗里德曼-斯科特在自传中披露",
}

plaintext: 测试结果
1
2
msgpack 消耗时间: 495.486364ms 秒
Json 消耗时间: 869.840604ms 秒

相差很明显,字段长度越大越明显。把字段加多:

Go: 字段加多
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var in = &ts{
    C:   "LOCK",
    K:   "31uEbMgunupShBVTewXjtqbBv5MndwfXhb",
    T:   1000,
    Max: 200,
    Cn:  "中文",
    Cn1: "中文",
    Cn2: "中文",
    Cn3: "中文",
    Cn4: "中文",
    Cn5: "中文",
    Cn6: "中文",
    Cn7: "中文",
    Cn8: "中文",
    Cn9: "中文",
}

plaintext: 测试结果
1
2
msgpack 消耗时间: 814.700585ms 秒
Json 消耗时间: 1.162055284s 秒

相差也很明显。顺便看一下 python:

Python: python 性能比较
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# -*- coding: utf-8 -*-

from time import time
import msgpack
import json
import ujson 

def exampleJson(ts):
    t1 = time()

    for i in xrange(100000):
        b = json.dumps(ts)
        out = json.loads(b)
    print 'json', time() - t1, 's'


def exampleUjson(ts):
    t1 = time()

    for i in xrange(100000):
        b = ujson.dumps(ts)
        out = ujson.loads(b)
    print 'ujson', time() - t1, 's'


def exampleMsgpack(ts):
    t1 = time()

    for i in xrange(100000):
        b = msgpack.packb(ts)
        out = msgpack.unpackb(b)
    print 'msgpack', time() - t1, 's'


if __name__ == "__main__":
    
    ts = {
        "C":   "LOCK",
        "K":   "31uEbMgunupShBVTewXjtqbBv5MndwfXhb",
        "T":   1000,
        "Max": 200,
        "Cn":  "中文",
        "Cn1": "中文",
        "Cn2": "中文",
        "Cn3": "中文",
        "Cn4": "中文",
        "Cn5": "中文",
        "Cn6": "中文",
        "Cn7": "中文",
        "Cn8": "中文",
        "Cn9": "中文",
    }
    exampleJson(ts)
    exampleUjson(ts)
    exampleMsgpack(ts)

输出:

plaintext: python 性能比较结果
1
2
3
json 2.1737818718 s
ujson 0.397536039352 s
msgpack 0.445297956467 s

python 和 go:

plaintext: python 和 go
1
2
3
py ujson > go msgpack x 2
py ujson > go json x 5
py json < go json x 2

python 里 ujson 和 msgpack 表现差不多,ujson 稍快一点点。

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

Related articles

Golang Range 的性能提升Tip

Go 语言里使用 range 可以方便遍历数组(array)、切片(slice)、字典(map)和信道(chan)。这里主要关注他们的性能。...

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

Write a Comment to "关于Golang msgpack 和 json 性能的简单比较"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms