Skip to content

Instantly share code, notes, and snippets.

@izderadicka
Last active September 23, 2022 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save izderadicka/e08e058591528d3775a5712e7174752f to your computer and use it in GitHub Desktop.
Save izderadicka/e08e058591528d3775a5712e7174752f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"messages = [\n",
" {'call_id': 1, 'kwargs': {}, 'args': ['sleep', 0.1]},\n",
" {'call_id': 1, 't': 'r', 'returned': 'd53b2823d35b471282ab5c8b6c2e4685'},\n",
" {'call_id': 2, 'kwargs': {'utc': True}, 'args': ['date', '%d-%m-%Y %H:%M %Z']},\n",
" {'call_id': 2, 't': 'r', 'returned': '77da239342e240a0a3078d50019a20a0'},\n",
" {'call_id': 1, 'data': {'status': 'started', 'task_id': 'd53b2823d35b471282ab5c8b6c2e4685'}, 't': 'm'},\n",
" {'call_id': 2, 'data': {'status': 'started', 'task_id': '77da239342e240a0a3078d50019a20a0'}, 't': 'm'},\n",
" {'call_id': 1, 'data': {'status': 'success', 'task_id': 'd53b2823d35b471282ab5c8b6c2e4685', 'result': None, 'duration': 0.12562298774719238}, 't': 'm'},\n",
" {'call_id': 2, 'data': {'status': 'success', 'task_id': '77da239342e240a0a3078d50019a20a0', 'result': '27-02-2017 11:46 UTC', 'duration': 0.04673957824707031}, 't': 'm'}\n",
" \n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import ujson\n",
"import msgpack\n",
"import umsgpack\n",
"import cbor\n",
"import ubjson\n",
"\n",
"NUM_ROUNDS = 10000"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# JSON (standard library)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total length 798\n"
]
}
],
"source": [
"sz=0\n",
"for m in messages:\n",
" sz+= len(json.dumps(m))\n",
"print('Total length %d'% sz)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"789 ms ± 34.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(NUM_ROUNDS):\n",
" for m in messages:\n",
" \n",
" json.loads(json.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# JSON (ujson)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total length 798\n"
]
}
],
"source": [
"sz=0\n",
"for m in messages:\n",
" sz+= len(json.dumps(m))\n",
"print('Total length %d'% sz)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"181 ms ± 8.71 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(NUM_ROUNDS):\n",
" for m in messages:\n",
" \n",
" ujson.loads(ujson.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MessagePack (official lib)"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total length 591\n"
]
}
],
"source": [
"sz=0\n",
"for m in messages:\n",
" sz+= len(msgpack.packb(m))\n",
"print('Total length %d'% sz)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"286 ms ± 12.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(NUM_ROUNDS):\n",
" for m in messages:\n",
" msgpack.unpackb(msgpack.packb(m))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MessagePack (umsgpack)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total length 585\n"
]
}
],
"source": [
"sz=0\n",
"for m in messages:\n",
" sz+= len(umsgpack.packb(m))\n",
"print('Total length %d'% sz)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"435 ms ± 12.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(NUM_ROUNDS):\n",
" for m in messages:\n",
" umsgpack.unpackb(umsgpack.packb(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CBOR"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total length 585\n"
]
}
],
"source": [
"sz=0\n",
"for m in messages:\n",
" sz+= len(cbor.dumps(m))\n",
"print('Total length %d'% sz)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"164 ms ± 6.04 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(NUM_ROUNDS):\n",
" for m in messages:\n",
" \n",
" cbor.loads(cbor.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# UBJSON"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total length 668\n"
]
}
],
"source": [
"sz=0\n",
"for m in messages:\n",
" sz+= len(ubjson.dumpb(m))\n",
"print('Total length %d'% sz)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"292 ms ± 15.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for i in range(NUM_ROUNDS):\n",
" for m in messages:\n",
" \n",
" ubjson.loadb(ubjson.dumpb(m))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment