Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.07 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "messages = [\n",
  12. " {'call_id': 1, 'kwargs': {}, 'args': ['sleep', 0.1]},\n",
  13. " {'call_id': 1, 't': 'r', 'returned': 'd53b2823d35b471282ab5c8b6c2e4685'},\n",
  14. " {'call_id': 2, 'kwargs': {'utc': True}, 'args': ['date', '%d-%m-%Y %H:%M %Z']},\n",
  15. " {'call_id': 2, 't': 'r', 'returned': '77da239342e240a0a3078d50019a20a0'},\n",
  16. " {'call_id': 1, 'data': {'status': 'started', 'task_id': 'd53b2823d35b471282ab5c8b6c2e4685'}, 't': 'm'},\n",
  17. " {'call_id': 2, 'data': {'status': 'started', 'task_id': '77da239342e240a0a3078d50019a20a0'}, 't': 'm'},\n",
  18. " {'call_id': 1, 'data': {'status': 'success', 'task_id': 'd53b2823d35b471282ab5c8b6c2e4685', 'result': None, 'duration': 0.12562298774719238}, 't': 'm'},\n",
  19. " {'call_id': 2, 'data': {'status': 'success', 'task_id': '77da239342e240a0a3078d50019a20a0', 'result': '27-02-2017 11:46 UTC', 'duration': 0.04673957824707031}, 't': 'm'}\n",
  20. " \n",
  21. "]"
  22. ]
  23. },
  24. {
  25. "cell_type": "code",
  26. "execution_count": 2,
  27. "metadata": {
  28. "collapsed": true
  29. },
  30. "outputs": [],
  31. "source": [
  32. "import json\n",
  33. "import ujson\n",
  34. "import msgpack\n",
  35. "import umsgpack\n",
  36. "import cbor\n",
  37. "import ubjson\n",
  38. "\n",
  39. "NUM_ROUNDS = 10000"
  40. ]
  41. },
  42. {
  43. "cell_type": "markdown",
  44. "metadata": {},
  45. "source": [
  46. "# JSON (standard library)"
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": 3,
  52. "metadata": {
  53. "collapsed": false
  54. },
  55. "outputs": [
  56. {
  57. "name": "stdout",
  58. "output_type": "stream",
  59. "text": [
  60. "Total length 798\n"
  61. ]
  62. }
  63. ],
  64. "source": [
  65. "sz=0\n",
  66. "for m in messages:\n",
  67. " sz+= len(json.dumps(m))\n",
  68. "print('Total length %d'% sz)"
  69. ]
  70. },
  71. {
  72. "cell_type": "code",
  73. "execution_count": 4,
  74. "metadata": {
  75. "collapsed": false
  76. },
  77. "outputs": [
  78. {
  79. "name": "stdout",
  80. "output_type": "stream",
  81. "text": [
  82. "1 loop, best of 3: 833 ms per loop\n"
  83. ]
  84. }
  85. ],
  86. "source": [
  87. "%%timeit\n",
  88. "for i in range(NUM_ROUNDS):\n",
  89. " for m in messages:\n",
  90. " \n",
  91. " json.loads(json.dumps(m))"
  92. ]
  93. },
  94. {
  95. "cell_type": "markdown",
  96. "metadata": {},
  97. "source": [
  98. "# JSON (ujson)"
  99. ]
  100. },
  101. {
  102. "cell_type": "code",
  103. "execution_count": 5,
  104. "metadata": {
  105. "collapsed": false
  106. },
  107. "outputs": [
  108. {
  109. "name": "stdout",
  110. "output_type": "stream",
  111. "text": [
  112. "Total length 798\n"
  113. ]
  114. }
  115. ],
  116. "source": [
  117. "sz=0\n",
  118. "for m in messages:\n",
  119. " sz+= len(json.dumps(m))\n",
  120. "print('Total length %d'% sz)"
  121. ]
  122. },
  123. {
  124. "cell_type": "code",
  125. "execution_count": 6,
  126. "metadata": {
  127. "collapsed": false
  128. },
  129. "outputs": [
  130. {
  131. "name": "stdout",
  132. "output_type": "stream",
  133. "text": [
  134. "1 loop, best of 3: 193 ms per loop\n"
  135. ]
  136. }
  137. ],
  138. "source": [
  139. "%%timeit\n",
  140. "for i in range(NUM_ROUNDS):\n",
  141. " for m in messages:\n",
  142. " \n",
  143. " ujson.loads(ujson.dumps(m))"
  144. ]
  145. },
  146. {
  147. "cell_type": "markdown",
  148. "metadata": {},
  149. "source": [
  150. "# MessagePack (official lib)"
  151. ]
  152. },
  153. {
  154. "cell_type": "code",
  155. "execution_count": 7,
  156. "metadata": {
  157. "collapsed": false
  158. },
  159. "outputs": [
  160. {
  161. "name": "stdout",
  162. "output_type": "stream",
  163. "text": [
  164. "Total length 591\n"
  165. ]
  166. }
  167. ],
  168. "source": [
  169. "sz=0\n",
  170. "for m in messages:\n",
  171. " sz+= len(msgpack.packb(m))\n",
  172. "print('Total length %d'% sz)"
  173. ]
  174. },
  175. {
  176. "cell_type": "code",
  177. "execution_count": 8,
  178. "metadata": {
  179. "collapsed": false
  180. },
  181. "outputs": [
  182. {
  183. "name": "stdout",
  184. "output_type": "stream",
  185. "text": [
  186. "1 loop, best of 3: 289 ms per loop\n"
  187. ]
  188. }
  189. ],
  190. "source": [
  191. "%%timeit\n",
  192. "for i in range(NUM_ROUNDS):\n",
  193. " for m in messages:\n",
  194. " msgpack.unpackb(msgpack.packb(m))\n"
  195. ]
  196. },
  197. {
  198. "cell_type": "markdown",
  199. "metadata": {},
  200. "source": [
  201. "# MessagePack (umsgpack)"
  202. ]
  203. },
  204. {
  205. "cell_type": "code",
  206. "execution_count": 9,
  207. "metadata": {
  208. "collapsed": false
  209. },
  210. "outputs": [
  211. {
  212. "name": "stdout",
  213. "output_type": "stream",
  214. "text": [
  215. "Total length 585\n"
  216. ]
  217. }
  218. ],
  219. "source": [
  220. "sz=0\n",
  221. "for m in messages:\n",
  222. " sz+= len(umsgpack.packb(m))\n",
  223. "print('Total length %d'% sz)"
  224. ]
  225. },
  226. {
  227. "cell_type": "code",
  228. "execution_count": 10,
  229. "metadata": {
  230. "collapsed": false
  231. },
  232. "outputs": [
  233. {
  234. "name": "stdout",
  235. "output_type": "stream",
  236. "text": [
  237. "1 loop, best of 3: 3.15 s per loop\n"
  238. ]
  239. }
  240. ],
  241. "source": [
  242. "%%timeit\n",
  243. "for i in range(NUM_ROUNDS):\n",
  244. " for m in messages:\n",
  245. " umsgpack.unpackb(umsgpack.packb(m))"
  246. ]
  247. },
  248. {
  249. "cell_type": "markdown",
  250. "metadata": {},
  251. "source": [
  252. "# CBOR"
  253. ]
  254. },
  255. {
  256. "cell_type": "code",
  257. "execution_count": 11,
  258. "metadata": {
  259. "collapsed": false
  260. },
  261. "outputs": [
  262. {
  263. "name": "stdout",
  264. "output_type": "stream",
  265. "text": [
  266. "Total length 585\n"
  267. ]
  268. }
  269. ],
  270. "source": [
  271. "sz=0\n",
  272. "for m in messages:\n",
  273. " sz+= len(cbor.dumps(m))\n",
  274. "print('Total length %d'% sz)"
  275. ]
  276. },
  277. {
  278. "cell_type": "code",
  279. "execution_count": 12,
  280. "metadata": {
  281. "collapsed": false
  282. },
  283. "outputs": [
  284. {
  285. "name": "stdout",
  286. "output_type": "stream",
  287. "text": [
  288. "1 loop, best of 3: 163 ms per loop\n"
  289. ]
  290. }
  291. ],
  292. "source": [
  293. "%%timeit\n",
  294. "for i in range(NUM_ROUNDS):\n",
  295. " for m in messages:\n",
  296. " \n",
  297. " cbor.loads(cbor.dumps(m))"
  298. ]
  299. },
  300. {
  301. "cell_type": "markdown",
  302. "metadata": {},
  303. "source": [
  304. "# UBJSON"
  305. ]
  306. },
  307. {
  308. "cell_type": "code",
  309. "execution_count": 13,
  310. "metadata": {
  311. "collapsed": false
  312. },
  313. "outputs": [
  314. {
  315. "name": "stdout",
  316. "output_type": "stream",
  317. "text": [
  318. "Total length 668\n"
  319. ]
  320. }
  321. ],
  322. "source": [
  323. "sz=0\n",
  324. "for m in messages:\n",
  325. " sz+= len(ubjson.dumpb(m))\n",
  326. "print('Total length %d'% sz)"
  327. ]
  328. },
  329. {
  330. "cell_type": "code",
  331. "execution_count": 14,
  332. "metadata": {
  333. "collapsed": false
  334. },
  335. "outputs": [
  336. {
  337. "name": "stdout",
  338. "output_type": "stream",
  339. "text": [
  340. "1 loop, best of 3: 2.28 s per loop\n"
  341. ]
  342. }
  343. ],
  344. "source": [
  345. "%%timeit\n",
  346. "for i in range(NUM_ROUNDS):\n",
  347. " for m in messages:\n",
  348. " \n",
  349. " ubjson.loadb(ubjson.dumpb(m))"
  350. ]
  351. }
  352. ],
  353. "metadata": {
  354. "kernelspec": {
  355. "display_name": "Python 3",
  356. "language": "python",
  357. "name": "python3"
  358. },
  359. "language_info": {
  360. "codemirror_mode": {
  361. "name": "ipython",
  362. "version": 3
  363. },
  364. "file_extension": ".py",
  365. "mimetype": "text/x-python",
  366. "name": "python",
  367. "nbconvert_exporter": "python",
  368. "pygments_lexer": "ipython3",
  369. "version": "3.5.1"
  370. }
  371. },
  372. "nbformat": 4,
  373. "nbformat_minor": 2
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement