Created
September 4, 2015 05:57
-
-
Save mcchae/5f15f7a156f0a675837a to your computer and use it in GitHub Desktop.
python serialization : dict <=> str
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#coding=utf8 | |
########################################################################################## | |
from datetime import datetime | |
import cPickle | |
import marshal | |
import msgpack | |
########################################################################################## | |
def do_eval(obj, limit): | |
sts = datetime.now() | |
for i in xrange(limit): | |
ds = str(obj) | |
ls = eval(ds) | |
if i == 0 and obj != ls: raise RuntimeError('obj revoke error!') | |
ets = datetime.now() | |
print('{} eval takes {}...'.format(limit, ets-sts)) | |
########################################################################################## | |
def do_pickle(obj, limit): | |
sts = datetime.now() | |
for i in xrange(limit): | |
ds = cPickle.dumps(obj) | |
ls = cPickle.loads(ds) | |
if i == 0 and obj != ls: raise RuntimeError('obj revoke error!') | |
ets = datetime.now() | |
print('{} eval takes {}...'.format(limit, ets-sts)) | |
########################################################################################## | |
def do_msgpack(obj, limit): | |
sts = datetime.now() | |
for i in xrange(limit): | |
ds = msgpack.packb(obj) | |
ls = msgpack.unpackb(ds) | |
if i == 0 and obj != ls: raise RuntimeError('obj revoke error!') | |
ets = datetime.now() | |
print('{} eval takes {}...'.format(limit, ets-sts)) | |
########################################################################################## | |
def do_marshal(obj, limit): | |
sts = datetime.now() | |
for i in xrange(limit): | |
ds = marshal.dumps(obj) | |
ls = marshal.loads(ds) | |
if i == 0 and obj != ls: raise RuntimeError('obj revoke error!') | |
ets = datetime.now() | |
print('{} eval takes {}...'.format(limit, ets-sts)) | |
########################################################################################## | |
if __name__ == '__main__': | |
limit = 100000 | |
obj = {'inpackets': 31125504, 'tunnel_error': 32, 'isregist': False, | |
'port_line_normal': True, 'cpu_util': 82, 'outpackets': 33349120, 'type': 17, | |
'isTunnelError': True, 'mem_util': 9, 'sessions': 5, 'activation': True, | |
'inkbps': 80352, 'corp': 'NNACF', 'name': '\xec\x9e_00162', | |
'center': 'internet', 'cid': 'f1c6c46e-8fc5-411f-b0b3-8f3061a58e1c', | |
'net_util': 72, 'droppackets': 894, 'outkbps': 52596, 'time': 1441181768, | |
'eth': [ | |
{'linkspeed': 0, 'inpackets': 0, 'ip': 0, 'util': 0, 'bandwidth': 0, | |
'num': 0, 'type': 'normal', 'ipsec': False, 'outbytes': 0, | |
'isLinkDown': True, 'inbytes': 0, 'dropbytes': 0, 'droppackets': 0, 'kbps': 0}, | |
{'linkspeed': 3703, 'inpackets': 257630, 'ip': 4578, 'util': 75, | |
'bandwidth': 78828262, 'num': 730, 'type': 'normal', 'ipsec': False, | |
'outbytes': 5545400, 'isLinkDown': False, 'inbytes': 4139600, | |
'dropbytes': 703, 'droppackets': 4, 'kbps': 891}, | |
{'linkspeed': 8571, 'inpackets': 143510, 'ip': 3835, 'util': 32, | |
'bandwidth': 90670011, 'num': 653, 'type': 'normal', 'ipsec': False, | |
'outbytes': 6588100, 'isLinkDown': False, 'inbytes': 1232100, | |
'dropbytes': 648, 'droppackets': 8, 'kbps': 4213}, | |
{'linkspeed': 0, 'inpackets': 0, 'ip': 0, 'util': 0, 'bandwidth': 0, | |
'num': 0, 'type': 'normal', 'ipsec': False, 'outbytes': 0, | |
'isLinkDown': True, 'inbytes': 0, 'dropbytes': 0, 'droppackets': 0, | |
'kbps': 0}, | |
{'linkspeed': 29801, 'inpackets': 588020, 'ip': 1952, 'util': 30, | |
'bandwidth': 83466247, 'num': 581, 'type': 'normal', 'ipsec': False, | |
'outbytes': 9945500, 'isLinkDown': False, 'inbytes': 3435800, | |
'dropbytes': 647, 'droppackets': 6, 'kbps': 1092}, | |
{'linkspeed': 0, 'inpackets': 0, 'ip': 0, 'util': 0, 'bandwidth': 0, | |
'num': 0, 'type': 'normal', 'ipsec': False, 'outbytes': 0, | |
'isLinkDown': True, 'inbytes': 0, 'dropbytes': 0, 'droppackets': 0, | |
'kbps': 0}, | |
{'linkspeed': 14359, 'inpackets': 557100, 'ip': 2921, 'util': 32, | |
'bandwidth': 18209429, 'num': 40, 'type': 'normal', 'ipsec': False, | |
'outbytes': 1155200, 'isLinkDown': False, 'inbytes': 8787100, | |
'dropbytes': 906, 'droppackets': 2, 'kbps': 5829}, | |
{'linkspeed': 6704, 'inpackets': 913340, 'ip': 2405, 'util': 73, | |
'bandwidth': 884716, 'num': 86, 'type': 'normal', 'ipsec': False, | |
'outbytes': 3145900, 'isLinkDown': False, 'inbytes': 5141200, | |
'dropbytes': 493, 'droppackets': 0, 'kbps': 11661}], 'dropkbps': 1} | |
do_eval(obj, limit) | |
do_pickle(obj, limit) | |
do_msgpack(obj, limit) | |
do_marshal(obj, limit) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment