Skip to content

Instantly share code, notes, and snippets.

@theY4Kman
Forked from izderadicka/Serialization tests.ipynb
Last active February 9, 2022 18:54
Show Gist options
  • Save theY4Kman/dcc40ecf7a4d7869ecb82b61c16741ee to your computer and use it in GitHub Desktop.
Save theY4Kman/dcc40ecf7a4d7869ecb82b61c16741ee to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"import base64\n",
"import os\n",
"\n",
"KB = 1024\n",
"MB = 1024 * KB\n",
"\n",
"messages = [\n",
" {'data': os.urandom(num_bytes)}\n",
" for num_bytes in [1*KB, 100*KB, 1*MB, 100*MB]\n",
"]\n",
"messages_base64 = [\n",
" {'data': base64.b64encode(message['data']).decode('ascii')}\n",
" for message in messages\n",
"]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import ujson\n",
"import orjson\n",
"import msgpack\n",
"import cbor\n",
"import ubjson"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"from humanfriendly import format_size\n",
"\n",
"def print_size(dumps, messages=messages):\n",
" size = sum(len(dumps(m)) for m in messages)\n",
" print('Total serialized size:', format_size(size))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# JSON (standard library)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total serialized size: 141.35 MB\n"
]
}
],
"source": [
"print_size(json.dumps, messages_base64)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"472 ms ± 1.62 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for m in messages_base64:\n",
" json.loads(json.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# JSON (ujson)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total serialized size: 143.55 MB\n"
]
}
],
"source": [
"print_size(ujson.dumps, messages_base64)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"779 ms ± 4.2 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for m in messages_base64:\n",
" ujson.loads(ujson.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ORJSON (orjson)"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total serialized size: 141.35 MB\n"
]
}
],
"source": [
"print_size(orjson.dumps, messages_base64)"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"238 ms ± 1.59 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"for m in messages_base64:\n",
" orjson.loads(orjson.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# MessagePack (official lib)"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total serialized size: 106.01 MB\n"
]
}
],
"source": [
"print_size(msgpack.packb)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"92.1 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for m in messages:\n",
" msgpack.unpackb(msgpack.packb(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# CBOR"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total serialized size: 106.01 MB\n"
]
}
],
"source": [
"print_size(cbor.dumps)"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"92.7 ms ± 576 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for m in messages:\n",
" cbor.loads(cbor.dumps(m))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# UBJSON"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total serialized size: 106.01 MB\n"
]
}
],
"source": [
"print_size(ubjson.dumpb)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"61.7 ms ± 461 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"for m in messages:\n",
" ubjson.loadb(ubjson.dumpb(m))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.10"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment