Created
August 9, 2013 15:32
-
-
Save prudnikov/6194586 to your computer and use it in GitHub Desktop.
Compare performance of `pickle`, `json` and MessagePack
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
import msgpack | |
import time | |
import json | |
import pickle | |
l = [1,2,3] | |
l_packed = msgpack.packb(l) | |
l_json = json.dumps(l) | |
l_pickle = pickle.dumps(l) | |
operations = 1000000 | |
def do_msgpack(): | |
start = time.time() | |
for i in xrange(operations): | |
msgpack.packb(l) | |
end = time.time() | |
print("MsgPack pack {0} of {2} done in {1}".format(operations, end-start, l)) | |
start = time.time() | |
for i in xrange(operations): | |
msgpack.unpackb(l_packed) | |
end = time.time() | |
print("MsgPack unpack {0} of {2} done in {1}".format(operations, end-start, l_packed)) | |
def do_json(): | |
start = time.time() | |
for i in xrange(operations): | |
json.dumps(l) | |
end = time.time() | |
print("JSON dumps {0} of {2} done in {1}".format(operations, end-start, l)) | |
start = time.time() | |
for i in xrange(operations): | |
json.loads(l_json) | |
end = time.time() | |
print("JSON loads {0} of {2} done in {1}".format(operations, end-start, l_json)) | |
def do_pickle(): | |
start = time.time() | |
for i in xrange(operations): | |
pickle.dumps(l) | |
end = time.time() | |
print("Pickle dumps {0} of {2} done in {1}".format(operations, end-start, l)) | |
start = time.time() | |
for i in xrange(operations): | |
pickle.loads(l_pickle) | |
end = time.time() | |
print("Pickle loads {0} of {2} done in {1}".format(operations, end-start, l_pickle)) | |
if __name__=="__main__": | |
do_msgpack() | |
do_json() | |
do_pickle() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment