一个关于Golang 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
输出:
1 2
相差不大,把 string 字段加长
1 2 3 4 5 6 7
var in = &ts{
C: "LOCK美国宇航局退休专家弗里德曼-斯科特在自传中披露",
K: "31uEbMgunupShBVTewXjtqbBv5MndwfXhb美国宇航局退休专家弗里德曼-斯科特在自传中披露",
T: 1000,
Max: 200,
Cn: "中文美国宇航局退休专家弗里德曼-斯科特在自传中披露",
}
1 2
相差很明显,字段长度越大越明显。把字段加多:
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: "中文",
}
1 2
相差也很明显。顺便看一下 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)
输出:
1 2 3
json 2.1737818718 s
ujson 0.397536039352 s
msgpack 0.445297956467 s
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 转摘请注明来源