Created
July 31, 2013 21:59
-
-
Save eykd/6126568 to your computer and use it in GitHub Desktop.
Python Serialization: Comparing the performance of msgpack, cPickle, and marshal.
This file contains hidden or 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
In [33]: msgpack_lt, pickle_lt, marshal_lt | |
Out[33]: (0.004015207290649414, 0.039834022521972656, 0.007205963134765625) | |
In [34]: msgpack_dt, pickle_dt, marshal_dt | |
Out[34]: (0.015387773513793945, 0.04079103469848633, 0.006851911544799805) | |
In [35]: len(s_msgpack), len(s_pickle), len(s_marshal) | |
Out[35]: (16, 48, 41) |
This file contains hidden or 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
import msgpack | |
import cPickle | |
import marshal | |
import timeit | |
NUM = 10000 | |
data = {'foo': 3, 'bar': 4, 'baz': 5} | |
e_msgpack_d = """msgpack.dumps(%s)""" % str(data) | |
s_msgpack = eval(e_msgpack_d) | |
e_msgpack_l = """msgpack.loads(%s)""" % repr(s_msgpack) | |
e_pickle_d = """cPickle.dumps(%s)""" % str(data) | |
s_pickle = eval(e_pickle_d) | |
e_pickle_l = """cPickle.loads(%s)""" % repr(s_pickle) | |
e_marshal_d = """marshal.dumps(%s)""" % str(data) | |
s_marshal = eval(e_marshal_d) | |
e_marshal_l = """marshal.loads(%s)""" % repr(s_marshal) | |
msgpack_dt = timeit.timeit(e_msgpack_d, "import msgpack", number=NUM) | |
pickle_dt = timeit.timeit(e_pickle_d, "import cPickle", number=NUM) | |
marshal_dt = timeit.timeit(e_marshal_d, "import marshal", number=NUM) | |
msgpack_lt = timeit.timeit(e_msgpack_l, "import msgpack", number=NUM) | |
pickle_lt = timeit.timeit(e_pickle_l, "import cPickle", number=NUM) | |
marshal_lt = timeit.timeit(e_marshal_l, "import marshal", number=NUM) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment