Python下各種格式的編碼效率初步測試(JSON,BSON, bz2, lzma,msgpack)

來源:互聯網
上載者:User

標籤:


# -*- coding:utf-8 -*-‘‘‘測試證明BSON和JSON所需要的位元組數相差無幾,甚至更多。壓縮演算法的效果取決於其冗餘程度。‘‘‘from __future__ import divisionimport collections  # From Python standard library.import datetimetest = {"name":"lemo", "age":12,        "address":{            "city":"suzhou",            "country":"china",            "code":215000} ,            #"timestamp":datetime.datetime.now(),    "scores":[{"name":"english","grade":3.0,"code":215000},              {"name":"chinese","grade":2.0,"code":215000},              {"name":"kerea","grade":3.0,"code":232000},              {"name":"france","grade":4.0,"code":235000},              {"name":"japanese","grade":4.0,"code":235000},              {"name":"kerea","grade":4.0,"code":235000},              {"name":"kerea","grade":4.0,"code":235300}    ]}#import bson#from bson.codec_options import CodecOptions#data = bson.BSON.encode(test)#print ("bson:", len(data) )#decoded_doc = bson.BSON.decode(data)import jsonjson_str = json.dumps( test )print ("json:",len(json_str) )### import bz2compressor = bz2.BZ2Compressor()compressor.compress(json_str.encode(‘utf-8‘)) bz2_str = compressor.flush() print ("bz2:",len(bz2_str),  len(bz2_str)/len(json_str) )import sysif sys.version_info.major*1000+ sys.version_info.minor>=3003:    import lzma    lzc = lzma.LZMACompressor()    out1 = lzc.compress(json_str.encode(‘utf-8‘) )    out2 = lzc.flush()    # Concatenate all the partial results:    result = b"".join([out1, out2])    print ("lzma:",len(result),  len(result)/len(json_str) )    lzd = lzma.LZMADecompressor()    dec_str = lzd.decompress(result )    assert json_str.encode(‘utf-8‘) == dec_str### msgpackimport datetimeimport msgpack#useful_dict = {    #"id": 1,    #"created": datetime.datetime.now(),#}def decode_datetime(obj):    if b‘__datetime__‘ in obj:        obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")    return objdef encode_datetime(obj):    if isinstance(obj, datetime.datetime):        return {‘__datetime__‘: True, ‘as_str‘: obj.strftime("%Y%m%dT%H:%M:%S.%f")}    return objpacked_dict = msgpack.packb(test, default=encode_datetime)print ("msgpack:",len(packed_dict),  len(packed_dict)/len(json_str) )#compressor = bz2.BZ2Compressor(compresslevel=1)#compressor.compress(packed_dict) #bz2_str = compressor.flush() #print ("msgpack_bz2:",len(bz2_str),  len(bz2_str)/len(json_str) )this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime)

Python下各種格式的編碼效率初步測試(JSON,BSON, bz2, lzma,msgpack)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.