Back to home

BSON vs Pickle

print timeit.Timer("""json.dumps({"a": Decimal("123123.123123123"), "b": datetime.now(), "c": {"1": 3213123, "2": "cccc"}}, use_decimal=True, default=json_util.default)""", "import simplejson as json; from datetime import datetime; from decimal import Decimal; from bson import json_util").timeit()
# 39.609019041061401


print timeit.Timer("""json.loads(a, object_hook=json_util.object_hook)""", """from bson import json_util; from datetime import datetime; from decimal import Decimal;import cPickle as pickle; import simplejson as json; a=json.dumps({"a": Decimal("123123.123123123"), "b": datetime.now(), "c": {"1": 3213123, "2": "cccc"}}, use_decimal=True, default=json_util.default)""").timeit()
# 3.5122969150543213 

print timeit.Timer("""pickle.dumps({"a": Decimal("123123.123123123"), "b": datetime.now(), "c": {"1": 3213123, "2": "cccc"}})""", "from datetime import datetime; from decimal import Decimal;import cPickle as pickle").timeit()
# 32.455498933792114

print timeit.Timer("""pickle.loads(a)""", """from datetime import datetime; from decimal import Decimal;import cPickle as pickle; a=pickle.dumps({"a": Decimal("123123.123123123"), "b": datetime.now(), "c": {"1": 3213123, "2": "cccc"}})""").timeit()
# 13.018862962722778

print timeit.Timer("""jsonpickle.encode({"a": Decimal("123123.123123123"), "b": datetime.now(), "c": {"1": 3213123, "2": "cccc"}})""", "import jsonpickle; from datetime import datetime; from decimal import Decimal;").timeit()
# 174.492460966