除了cPickle,cjson外還有沒有更高效點的序列化庫了
阿新 • • 發佈:2019-02-07
msgpack最快,而且是跨語言的,二進位制,但只能打包簡單的list,dict,int,string,unicode,在memcache之類的
字串協議裡會有問題。
marshal其次,也是二進位制的,可以打包大多數python物件,缺點同msgpack,另文件上說各版本的python實現會不一樣,但我這測下
來三臺機器分別是stackless 2.6.5 2.7.2 python 2.6.2之間都沒出現相容問題
tnetstring速度第三,字元打包,可以完美支援各種協議的後端,但有一個致命的缺點就是支援型別太少,比如下面的程式碼mongo裡出來的
unicode就無法打包。
以上三者速度差距很小很小。
bson,cPickle,json都很慢。。直接pass
cPickle_test used time 1.32382702827s
marshal_test used time 0.29944396019s
msgpackb_test used time 0.23744893074s
#!/usr/bin/env Python
#coding=utf-8
'''
Created on Jul 13, 2011
@author: apple
'''
import time,functools
import cPickle,marshal,tnetstring,bson,msgpack
import pymongo
data = pymongo.Connection().freeasker.question.find_one()
data.pop('_id')
def timeit(func):
@functools.wraps(func)
def __do__(*args,**wk):
start = time.time()
result= func(*args,**wk)
print '%s used time %ss'%(func.__name__,time.time()-start)
return result
return __do__
@timeit
def cPickle_test(c,data):
for i in xrange(c):
s = cPickle.dumps(data)
cPickle.loads(s)
@timeit
def marshal_test(c,data):
for i in xrange(c):
s = marshal.dumps(data)
marshal.loads(s)
@timeit
def tnetstring_test(c,data):
for i in xrange(c):
s = tnetstring.dumps(data)
tnetstring.loads(s)
@timeit
def bson_test(c,data):
for i in xrange(c):
s = bson.dumps(data)
bson.loads(s)
@timeit
def msgpackb_test(c,data):
# packer = msgpack.Packer()
for i in xrange(c):
s = msgpack.packb(data)
msgpack.unpackb(s)
@timeit
def msgpack_test(c,data):
packer = msgpack.Packer()
for i in xrange(c):
s = packer.pack(data)
packer.unpack(s)
if __name__ == '__main__':
count = 10000
data = pymongo.Connection().freeasker.question.find_one()
data.pop('_id')
cPickle_test(count, data)
marshal_test(count, data)
# tnetstring_test(count, data)
# bson_test(count, data)
# msgpack_test(count, data)
msgpackb_test(count, data)
字串協議裡會有問題。
marshal其次,也是二進位制的,可以打包大多數python物件,缺點同msgpack,另文件上說各版本的python實現會不一樣,但我這測下
來三臺機器分別是stackless 2.6.5 2.7.2 python 2.6.2之間都沒出現相容問題
tnetstring速度第三,字元打包,可以完美支援各種協議的後端,但有一個致命的缺點就是支援型別太少,比如下面的程式碼mongo裡出來的
unicode就無法打包。
以上三者速度差距很小很小。
bson,cPickle,json都很慢。。直接pass
cPickle_test used time 1.32382702827s
marshal_test used time 0.29944396019s
msgpackb_test used time 0.23744893074s
#!/usr/bin/env Python
#coding=utf-8
'''
Created on Jul 13, 2011
@author: apple
'''
import time,functools
import cPickle,marshal,tnetstring,bson,msgpack
import pymongo
data = pymongo.Connection().freeasker.question.find_one()
data.pop('_id')
def timeit(func):
@functools.wraps(func)
def __do__(*args,**wk):
start = time.time()
result= func(*args,**wk)
print '%s used time %ss'%(func.__name__,time.time()-start)
return result
return __do__
@timeit
def cPickle_test(c,data):
for i in xrange(c):
s = cPickle.dumps(data)
cPickle.loads(s)
@timeit
def marshal_test(c,data):
for i in xrange(c):
s = marshal.dumps(data)
marshal.loads(s)
@timeit
def tnetstring_test(c,data):
for i in xrange(c):
s = tnetstring.dumps(data)
tnetstring.loads(s)
@timeit
def bson_test(c,data):
for i in xrange(c):
s = bson.dumps(data)
bson.loads(s)
@timeit
def msgpackb_test(c,data):
# packer = msgpack.Packer()
for i in xrange(c):
s = msgpack.packb(data)
msgpack.unpackb(s)
@timeit
def msgpack_test(c,data):
packer = msgpack.Packer()
for i in xrange(c):
s = packer.pack(data)
packer.unpack(s)
if __name__ == '__main__':
count = 10000
data = pymongo.Connection().freeasker.question.find_one()
data.pop('_id')
cPickle_test(count, data)
marshal_test(count, data)
# tnetstring_test(count, data)
# bson_test(count, data)
# msgpack_test(count, data)
msgpackb_test(count, data)