Created
June 23, 2014 19:20
-
-
Save yuyichao/06ec3873fdcdca0f604b to your computer and use it in GitHub Desktop.
Implicit decode benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:e02d00fa7e69f9c3faa99777646c177517bfb3e32ed2a82e0086422336cbeb48" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def convert1(s):\n", | |
" if isinstance(s, str):\n", | |
" return s\n", | |
" elif isinstance(s, unicode):\n", | |
" return s.encode('utf8')\n", | |
"\n", | |
"def convert2(s):\n", | |
" if isinstance(s, unicode):\n", | |
" return s.encode('utf8')\n", | |
" elif isinstance(s, str):\n", | |
" return s\n", | |
"\n", | |
"def convert3(s):\n", | |
" if isinstance(s, basestring):\n", | |
" return s.encode('utf8')\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# cpython2\n", | |
"print('bytes')\n", | |
"%timeit convert1(b'111111111111111111111111111')\n", | |
"%timeit convert2(b'111111111111111111111111111')\n", | |
"%timeit convert3(b'111111111111111111111111111')\n", | |
"print('unicode')\n", | |
"%timeit convert1(u'111111111111111111111111111')\n", | |
"%timeit convert2(u'111111111111111111111111111')\n", | |
"%timeit convert3(u'111111111111111111111111111')\n", | |
"\n", | |
"long_bytes = b'123456789abcdef' * 100\n", | |
"long_unicode = u'123456789abcdef' * 100\n", | |
"\n", | |
"print('long bytes')\n", | |
"%timeit convert1(long_bytes)\n", | |
"%timeit convert2(long_bytes)\n", | |
"%timeit convert3(long_bytes)\n", | |
"print('long unicode')\n", | |
"%timeit convert1(long_unicode)\n", | |
"%timeit convert2(long_unicode)\n", | |
"%timeit convert3(long_unicode)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"bytes\n", | |
"10000000 loops, best of 3: 157 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 333 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 737 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"unicode\n", | |
"1000000 loops, best of 3: 726 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 529 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 604 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"long bytes\n", | |
"10000000 loops, best of 3: 161 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 349 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"100000 loops, best of 3: 4.98 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"long unicode\n", | |
"1000000 loops, best of 3: 1.77 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 1.58 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"1000000 loops, best of 3: 1.65 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# pypy2\n", | |
"print('bytes')\n", | |
"%timeit convert1(b'111111111111111111111111111')\n", | |
"%timeit convert2(b'111111111111111111111111111')\n", | |
"%timeit convert3(b'111111111111111111111111111')\n", | |
"print('unicode')\n", | |
"%timeit convert1(u'111111111111111111111111111')\n", | |
"%timeit convert2(u'111111111111111111111111111')\n", | |
"%timeit convert3(u'111111111111111111111111111')\n", | |
"\n", | |
"long_bytes = b'123456789abcdef' * 100\n", | |
"long_unicode = u'123456789abcdef' * 100\n", | |
"\n", | |
"print('long bytes')\n", | |
"%timeit convert1(long_bytes)\n", | |
"%timeit convert2(long_bytes)\n", | |
"%timeit convert3(long_bytes)\n", | |
"print('long unicode')\n", | |
"%timeit convert1(long_unicode)\n", | |
"%timeit convert2(long_unicode)\n", | |
"%timeit convert3(long_unicode)\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"bytes\n", | |
"100000000 loops, best of 3: 2.36 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"100000000 loops, best of 3: 2.3 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"10000000 loops, best of 3: 136 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"unicode\n", | |
"10000000 loops, best of 3: 64.9 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"10000000 loops, best of 3: 65.7 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"10000000 loops, best of 3: 65.5 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"long bytes\n", | |
"100000000 loops, best of 3: 2.22 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"100000000 loops, best of 3: 2.33 ns per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"100000 loops, best of 3: 7.46 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"long unicode\n", | |
"100000 loops, best of 3: 3.17 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"100000 loops, best of 3: 3.19 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n", | |
"100000 loops, best of 3: 3.19 \u00b5s per loop" | |
] | |
}, | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"\n" | |
] | |
} | |
], | |
"prompt_number": 3 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment