Skip to content

Instantly share code, notes, and snippets.

@justinfx
Last active October 16, 2022 13:28
Show Gist options
  • Save justinfx/3174062 to your computer and use it in GitHub Desktop.
Save justinfx/3174062 to your computer and use it in GitHub Desktop.
Speed test of common serializers on python 2.7.2 (pickle, cPickle, ujson, cjson, simplejson, json, yajl, msgpack)
"""
Dependencies:
pip install tabulate simplejson ujson yajl msgpack
"""
from timeit import timeit
from tabulate import tabulate
setup = '''d = {
'words': """
Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Mauris adipiscing adipiscing placerat.
Vestibulum augue augue,
pellentesque quis sollicitudin id, adipiscing.
""",
'list': list(range(100)),
'dict': dict((str(i),'a') for i in range(100)),
'int': 100,
'float': 100.123456
}'''
setup_pickle = '%s ; import pickle ; src = pickle.dumps(d)' % setup
setup_pickle2 = '%s ; import pickle ; src = pickle.dumps(d, 2)' % setup
setup_pickle3 = '%s ; import pickle ; src = pickle.dumps(d, 3)' % setup
setup_pickle4 = '%s ; import pickle ; src = pickle.dumps(d, 4)' % setup
setup_json = '%s ; import json; src = json.dumps(d)' % setup
setup_msgpack = '%s ; src = msgpack.dumps(d)' % setup
tests = [
# (title, setup, enc_test, dec_test)
('pickle (ascii)', 'import pickle; %s' % setup_pickle, 'pickle.dumps(d, 0)', 'pickle.loads(src)'),
('pickle (binary-2)', 'import pickle; %s' % setup_pickle2, 'pickle.dumps(d, 2)', 'pickle.loads(src)'),
('pickle (binary-3)', 'import pickle; %s' % setup_pickle3, 'pickle.dumps(d, 3)', 'pickle.loads(src)'),
('pickle (binary-4)', 'import pickle; %s' % setup_pickle4, 'pickle.dumps(d, 4)', 'pickle.loads(src)'),
('json', 'import json; %s' % setup_json, 'json.dumps(d)', 'json.loads(src)'),
('simplejson-3.17.0', 'import simplejson; %s' % setup_json, 'simplejson.dumps(d)', 'simplejson.loads(src)'),
('ujson-1.35', 'import ujson; %s' % setup_json, 'ujson.dumps(d)', 'ujson.loads(src)'),
('yajl 0.3.5', 'import yajl; %s' % setup_json, 'yajl.dumps(d)', 'yajl.loads(src)'),
('msgpack-0.6.2', 'import msgpack; %s' % setup_msgpack, 'msgpack.dumps(d)', 'msgpack.loads(src)'),
]
loops = 15000
enc_table = []
dec_table = []
print("Running tests (%d loops each)" % loops)
for title, mod, enc, dec in tests:
print(title)
print(" [Encode]", enc)
result = timeit(enc, mod, number=loops)
enc_table.append([title, result])
print(" [Decode]", dec)
result = timeit(dec, mod, number=loops)
dec_table.append([title, result])
enc_table.sort(key=lambda x: x[1])
enc_table.insert(0, ['Package', 'Seconds'])
dec_table.sort(key=lambda x: x[1])
dec_table.insert(0, ['Package', 'Seconds'])
print("\nEncoding Test (%d loops)" % loops)
print(tabulate(enc_table, headers="firstrow"))
print("\nDecoding Test (%d loops)" % loops)
print(tabulate(dec_table, headers="firstrow"))
"""
Running tests (15000 loops each)
pickle (ascii)
[Encode] pickle.dumps(d, 0)
[Decode] pickle.loads(src)
pickle (binary-2)
[Encode] pickle.dumps(d, 2)
[Decode] pickle.loads(src)
pickle (binary-3)
[Encode] pickle.dumps(d, 3)
[Decode] pickle.loads(src)
pickle (binary-4)
[Encode] pickle.dumps(d, 4)
[Decode] pickle.loads(src)
json
[Encode] json.dumps(d)
[Decode] json.loads(src)
simplejson-3.17.0
[Encode] simplejson.dumps(d)
[Decode] simplejson.loads(src)
ujson-1.35
[Encode] ujson.dumps(d)
[Decode] ujson.loads(src)
yajl 0.3.5
[Encode] yajl.dumps(d)
[Decode] yajl.loads(src)
msgpack-0.6.2
[Encode] msgpack.dumps(d)
[Decode] msgpack.loads(src)
Encoding Test (15000 loops)
Package Seconds
----------------- ---------
pickle (binary-2) 0.141743
pickle (binary-3) 0.149022
pickle (binary-4) 0.151039
msgpack-0.6.2 0.155419
ujson-1.35 0.180287
yajl 0.3.5 0.388522
json 0.440383
simplejson-3.17.0 0.484677
pickle (ascii) 0.661336
Decoding Test (15000 loops)
Package Seconds
----------------- ---------
msgpack-0.6.2 0.136735
pickle (binary-4) 0.172758
pickle (ascii) 0.173402
pickle (binary-3) 0.175427
pickle (binary-2) 0.178137
ujson-1.35 0.206923
json 0.328846
yajl 0.3.5 0.438112
simplejson-3.17.0 0.475276
"""
@dec1985
Copy link

dec1985 commented Sep 10, 2014

Thanks.

@LyleScott
Copy link

Time to learn some msgpack!

@helinwang
Copy link

helinwang commented May 30, 2017

Thanks! Here is an updated test result on docker image python:2.7. The packages are installed using pip install tabulate simplejson python-cjson ujson yajl msgpack-python (I think the package versions in the log do not reflect real version).

Running tests (15000 loops each)
pickle (ascii)
  [Encode] pickle.dumps(d, 0)
  [Decode] pickle.loads(src)
pickle (binary)
  [Encode] pickle.dumps(d, 2)
  [Decode] pickle.loads(src)
cPickle (ascii)
  [Encode] cPickle.dumps(d, 0)
  [Decode] cPickle.loads(src)
cPickle (binary)
  [Encode] cPickle.dumps(d, 2)
  [Decode] cPickle.loads(src)
json
  [Encode] json.dumps(d)
  [Decode] json.loads(src)
simplejson-3.3.1
  [Encode] simplejson.dumps(d)
  [Decode] simplejson.loads(src)
python-cjson-1.0.5
  [Encode] cjson.encode(d)
  [Decode] cjson.decode(src)
ujson-1.33
  [Encode] ujson.dumps(d)
  [Decode] ujson.loads(src)
yajl 0.3.5
  [Encode] yajl.dumps(d)
  [Decode] yajl.loads(src)
msgpack-python-0.3.0
  [Encode] msgpack.dumps(d)
  [Decode] msgpack.loads(src)

Encoding Test (15000 loops)
Package                 Seconds
--------------------  ---------
cPickle (binary)       0.174544
msgpack-python-0.3.0   0.205447
yajl 0.3.5             0.302159
python-cjson-1.0.5     0.332643
json                   0.399422
ujson-1.33             0.464856
simplejson-3.3.1       0.658939
cPickle (ascii)        0.867376
pickle (ascii)         6.64729
pickle (binary)        6.72656

Decoding Test (15000 loops)
Package                 Seconds
--------------------  ---------
msgpack-python-0.3.0   0.126161
cPickle (binary)       0.223152
python-cjson-1.0.5     0.236071
ujson-1.33             0.244355
simplejson-3.3.1       0.352405
yajl 0.3.5             0.356132
cPickle (ascii)        0.654795
json                   0.923114
pickle (binary)        2.46238
pickle (ascii)         7.38126

@SeverinAlexB
Copy link

Here is the update code and results for Python3.5.
cPickle and cjson have been removed.

from timeit import timeit
from tabulate import tabulate

setup = '''d = {
    'words': """
        Lorem ipsum dolor sit amet, consectetur adipiscing 
        elit. Mauris adipiscing adipiscing placerat. 
        Vestibulum augue augue, 
        pellentesque quis sollicitudin id, adipiscing.
        """,
    'list': list(range(100)),
    'dict': dict((str(i),'a') for i in range(100)),
    'int': 100,
    'float': 100.123456
}'''

setup_pickle    = '%s ; import pickle ; src = pickle.dumps(d)' % setup
setup_pickle2   = '%s ; import pickle ; src = pickle.dumps(d, 2)' % setup
setup_json      = '%s ; import json; src = json.dumps(d)' % setup
setup_msgpack   = '%s ; src = msgpack.dumps(d)' % setup

tests = [
    # (title, setup, enc_test, dec_test)
    ('pickle (ascii)', 'import pickle; %s' % setup_pickle, 'pickle.dumps(d, 0)', 'pickle.loads(src)'),
    ('pickle (binary)', 'import pickle; %s' % setup_pickle2, 'pickle.dumps(d, 2)', 'pickle.loads(src)'),
    ('json', 'import json; %s' % setup_json, 'json.dumps(d)', 'json.loads(src)'),
    ('simplejson-3.3.1', 'import simplejson; %s' % setup_json, 'simplejson.dumps(d)', 'simplejson.loads(src)'),
    ('ujson-1.33', 'import ujson; %s' % setup_json, 'ujson.dumps(d)', 'ujson.loads(src)'),
    ('yajl 0.3.5', 'import yajl; %s' % setup_json, 'yajl.dumps(d)', 'yajl.loads(src)'),
    ('msgpack-python-0.3.0', 'import msgpack; %s' % setup_msgpack, 'msgpack.dumps(d)', 'msgpack.loads(src)'),
]

loops = 15000
enc_table = []
dec_table = []

print("Running tests (%d loops each)" % loops)

for title, mod, enc, dec in tests:
    print(title)

    print("  [Encode]", enc)
    result = timeit(enc, mod, number=loops)
    enc_table.append([title, result])

    print("  [Decode]", dec)
    result = timeit(dec, mod, number=loops)
    dec_table.append([title, result])

enc_table.sort(key=lambda x: x[1])
enc_table.insert(0, ['Package', 'Seconds'])

dec_table.sort(key=lambda x: x[1])
dec_table.insert(0, ['Package', 'Seconds'])

print("\nEncoding Test (%d loops)" % loops)
print(tabulate(enc_table, headers="firstrow"))

print("\nDecoding Test (%d loops)" % loops)
print(tabulate(dec_table, headers="firstrow"))

Results:

Running tests (15000 loops each)
pickle (ascii)
  [Encode] pickle.dumps(d, 0)
  [Decode] pickle.loads(src)
pickle (binary)
  [Encode] pickle.dumps(d, 2)
  [Decode] pickle.loads(src)
json
  [Encode] json.dumps(d)
  [Decode] json.loads(src)
simplejson-3.3.1
  [Encode] simplejson.dumps(d)
  [Decode] simplejson.loads(src)
ujson-1.33
  [Encode] ujson.dumps(d)
  [Decode] ujson.loads(src)
yajl 0.3.5
  [Encode] yajl.dumps(d)
  [Decode] yajl.loads(src)
msgpack-python-0.3.0
  [Encode] msgpack.dumps(d)
  [Decode] msgpack.loads(src)

Encoding Test (15000 loops)
Package                 Seconds
--------------------  ---------
pickle (binary)        0.143126
ujson-1.33             0.207734
json                   0.469505
yajl 0.3.5             0.470457
msgpack-python-0.3.0   0.471562
pickle (ascii)         0.664861
simplejson-3.3.1       0.678065

Decoding Test (15000 loops)
Package                 Seconds
--------------------  ---------
msgpack-python-0.3.0   0.170831
ujson-1.33             0.251938
pickle (ascii)         0.258618
pickle (binary)        0.292305
yajl 0.3.5             0.463486
json                   0.495874
simplejson-3.3.1       0.63921

@arturoribes
Copy link

How can it be that encoding with msgpack went from 0.12 seconds in the previous test to 0.47 seconds?

@JellyWX
Copy link

JellyWX commented Dec 15, 2017

Here's results for default JSON, Pickle, MsgPack and uMsgPack (Python 3.5):

Encoding Test (30000 loops)
Package                  Seconds
---------------------  ---------
pickle (binary)         0.675608
umsgpack-python-0.4.8   1.37743
msgpack-python-0.4.8    1.38327
json                    2.23619
pickle (ascii)          2.78601

Decoding Test (30000 loops)
Package                  Seconds
---------------------  ---------
msgpack-python-0.4.8    0.842506
umsgpack-python-0.4.8   0.847567
pickle (binary)         1.32874
pickle (ascii)          1.34376
json                    2.95099

Edit: Something was wrong.
UMsgPack is actually much much slower when used from the file.
Here's an updated report:

Encoding Test (30000 loops)
Package                  Seconds
---------------------  ---------
pickle (binary)         0.661877
msgpack-python-0.4.8    1.41231
json                    2.20819
pickle (ascii)          2.7429
umsgpack (WGET) 33.3858

Decoding Test (30000 loops)
Package                  Seconds
---------------------  ---------
msgpack-python-0.4.8    0.815752
pickle (ascii)          1.29843
pickle (binary)         1.31096
json                    2.96718
umsgpack (WGET)  40.3615

@halsafar
Copy link

I would also like to know how msg pack suddenly went from the winner to worse than pythons native pickle. Python3 pickle must be heavily optimized.

@justinfx
Copy link
Author

I've updated the test and ran them again against python-3.7.5.
msgpack-python has been deprecated in favor of msgpack, which is now the fastest again. It uses cython bindings which means it should be faster than any pure python solution. You can also now see the difference between the different binary protocol levels of pickle.

Running tests (15000 loops each)
pickle (ascii)
  [Encode] pickle.dumps(d, 0)
  [Decode] pickle.loads(src)
pickle (binary-2)
  [Encode] pickle.dumps(d, 2)
  [Decode] pickle.loads(src)
pickle (binary-3)
  [Encode] pickle.dumps(d, 3)
  [Decode] pickle.loads(src)
pickle (binary-4)
  [Encode] pickle.dumps(d, 4)
  [Decode] pickle.loads(src)
json
  [Encode] json.dumps(d)
  [Decode] json.loads(src)
simplejson-3.17.0
  [Encode] simplejson.dumps(d)
  [Decode] simplejson.loads(src)
ujson-1.35
  [Encode] ujson.dumps(d)
  [Decode] ujson.loads(src)
yajl 0.3.5
  [Encode] yajl.dumps(d)
  [Decode] yajl.loads(src)
msgpack-0.6.2
  [Encode] msgpack.dumps(d)
  [Decode] msgpack.loads(src)

Encoding Test (15000 loops)
Package              Seconds
-----------------  ---------
pickle (binary-2)   0.141743
pickle (binary-3)   0.149022
pickle (binary-4)   0.151039
msgpack-0.6.2       0.155419
ujson-1.35          0.180287
yajl 0.3.5          0.388522
json                0.440383
simplejson-3.17.0   0.484677
pickle (ascii)      0.661336

Decoding Test (15000 loops)
Package              Seconds
-----------------  ---------
msgpack-0.6.2       0.136735
pickle (binary-4)   0.172758
pickle (ascii)      0.173402
pickle (binary-3)   0.175427
pickle (binary-2)   0.178137
ujson-1.35          0.206923
json                0.328846
yajl 0.3.5          0.438112
simplejson-3.17.0   0.475276

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment