Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "collapsed": true
- },
- "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": 2,
- "metadata": {
- "collapsed": true
- },
- "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": 3,
- "metadata": {
- "collapsed": false
- },
- "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": 4,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 loop, best of 3: 833 ms per loop\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": 5,
- "metadata": {
- "collapsed": false
- },
- "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": 6,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 loop, best of 3: 193 ms per loop\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": 7,
- "metadata": {
- "collapsed": false
- },
- "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": 8,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 loop, best of 3: 289 ms per loop\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": 9,
- "metadata": {
- "collapsed": false
- },
- "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": 10,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 loop, best of 3: 3.15 s per loop\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": 11,
- "metadata": {
- "collapsed": false
- },
- "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": 12,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 loop, best of 3: 163 ms per loop\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": 13,
- "metadata": {
- "collapsed": false
- },
- "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": 14,
- "metadata": {
- "collapsed": false
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1 loop, best of 3: 2.28 s per loop\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.1"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement