关于Golang msgpack 和 json 性能的简单比较
🕘 Sun, 27 Dec 2015 by GolangNote
一个关于Golang msgpack 和 json 性能的简单比较:
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), "秒")
}
输出:
msgpack 消耗时间: 409.721398ms 秒
Json 消耗时间: 446.545891ms 秒
相差不大,把string 字段加长
var in = &ts{
C: "LOCK美国宇航局退休专家弗里德曼-斯科特在自传中披露",
K: "31uEbMgunupShBVTewXjtqbBv5MndwfXhb美国宇航局退休专家弗里德曼-斯科特在自传中披露",
T: 1000,
Max: 200,
Cn: "中文美国宇航局退休专家弗里德曼-斯科特在自传中披露",
}
msgpack 消耗时间: 495.486364ms 秒
Json 消耗时间: 869.840604ms 秒
相差很明显,字段长度越大越明显。把字段加多:
var in = &ts{
C: "LOCK",
K: "31uEbMgunupShBVTewXjtqbBv5MndwfXhb",
T: 1000,
Max: 200,
Cn: "中文",
Cn1: "中文",
Cn2: "中文",
Cn3: "中文",
Cn4: "中文",
Cn5: "中文",
Cn6: "中文",
Cn7: "中文",
Cn8: "中文",
Cn9: "中文",
}
msgpack 消耗时间: 814.700585ms 秒
Json 消耗时间: 1.162055284s 秒
相差也很明显。顺便看一下python:
# -*- 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)
输出:
json 2.1737818718 s
ujson 0.397536039352 s
msgpack 0.445297956467 s
python 和 go:
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 (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Relative Articles
Recent Go Articles
- 谷歌翻译的 golang 库推荐
- Go 1.13.2 与1.13.3 紧急更新
- golang 人脸检测识别库
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang phantomjs 动态代理实现
- Golang chrome debug protocol 库推荐
- Golang 随机打乱数组/Slice
- Golang sync.WaitGroup 的 Wait 超时处理
- Golang实现简单的Socks5代理
- Golang 用snappy + Base64 简单压缩加密进行网络传输
- Golang http IPv4/IPv6 服务
- golang 全角半角相互转换
- 在自己的网站部署TLS 1.3
- Golang 实现/打印菜单树
- Golang telegram 机器人小试
- Golang get 请求忽略数字证书进行校验
- Golang字符串相等比较的性能
- golang对输入表单简单验证
- 提高go运行性能小技巧
- golog: 一个快速的、非结构化日志库
Top Go Articles
- Golang Leveldb 基本使用的例子
- golang image 智能合并图片
- golang 把上传文件转为byte
- Go build 错误 “stackcheck redeclared in this block previous declaration”的解决方法
- Golang字符串相等比较的性能
- Golang Json/map 合并/update
- fmtsort: go1.12 中将内置按map key 排序输出
- mismatched types time.Duration and int
- golang 把http.Client 读取的图片直接放到image.Decode 处理
- Golang 随机打乱数组/Slice
- golog: 一个快速的、非结构化日志库
- 在自己的网站部署TLS 1.3
- Golang http.Post 用最小的内存发送大文件
- golang gencode 序列化/反序列化数据
- golang rot13 简单加密字符
- golang 上传大文件并获取其大小的例子
- bolt 使用示例
- Caddy、 SSLDocker、Nginx 性能比较及使用体验
- Golang io.ReadCloser 和[]byte 相互转化的方法
- golang 生成良好的唯一ID/uuid库比较