Last active
December 22, 2015 15:19
-
-
Save vanzaj/6492016 to your computer and use it in GitHub Desktop.
timing of str concat in python
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": "" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"based on http://www.skymind.com/~ocrow/python_string/" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import sys\n", | |
"print sys.version" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"2.7.5 |Anaconda 1.6.1 (x86_64)| (default, Jun 28 2013, 22:20:13) \n", | |
"[GCC 4.0.1 (Apple Inc. build 5493)]\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"MAX_NUM = 20000" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def method_0_str(max_num=MAX_NUM):\n", | |
" out_str = ''\n", | |
" for num in xrange(max_num):\n", | |
" out_str += str(num)\n", | |
" return out_str" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 3 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def method_0_repr(max_num=MAX_NUM):\n", | |
" out_str = ''\n", | |
" for num in xrange(max_num):\n", | |
" out_str += repr(num)\n", | |
" return out_str" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def method_1_bt(max_num=MAX_NUM):\n", | |
" out_str = ''\n", | |
" for num in xrange(max_num):\n", | |
" out_str += `num`\n", | |
" return out_str" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from array import array\n", | |
"\n", | |
"def method_3_array(max_num=MAX_NUM):\n", | |
" char_array = array('c') \n", | |
" for num in xrange(max_num):\n", | |
" char_array.fromstring(repr(num))\n", | |
" return char_array.tostring()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 6 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def method_4_list(max_num=MAX_NUM):\n", | |
" str_list = []\n", | |
" for num in xrange(max_num):\n", | |
" str_list.append(repr(num))\n", | |
" return ''.join(str_list)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from cStringIO import StringIO\n", | |
"\n", | |
"def method_5_cStr(max_num=MAX_NUM):\n", | |
" file_str = StringIO()\n", | |
" for num in xrange(max_num):\n", | |
" file_str.write(repr(num))\n", | |
" return file_str.getvalue()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def method_6_list_compr(max_num=MAX_NUM):\n", | |
" return ''.join([repr(num) for num in xrange(max_num)])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 9 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def method_7_buffer(max_num=MAX_NUM):\n", | |
" templist = [0]*max_num\n", | |
" num = 0\n", | |
" while num < max_num:\n", | |
" templist[num] = repr(num)\n", | |
" num += 1\n", | |
" return ''.join(templist)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 10 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"# a bit of cheating\n", | |
"import numpy as np\n", | |
"def method_8_numpy(max_num=MAX_NUM):\n", | |
" arr = np.arange(max_num)\n", | |
" return np.array2string(arr, separator='')" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"method_5_cStr(10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 12, | |
"text": [ | |
"'0123456789'" | |
] | |
} | |
], | |
"prompt_number": 12 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## timing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import time\n", | |
"def _timeit(stmt, repeat=1):\n", | |
" code = compile(stmt, '<_timeit>', 'exec')\n", | |
" t0 = time.time() # use time.clock() if on windows\n", | |
" exec code\n", | |
" t1 = time.time()\n", | |
" ms = (t1-t0)*1000\n", | |
" \n", | |
" cnt = 1\n", | |
" while cnt < repeat:\n", | |
" t0 = time.time() # use time.clock() if on windows\n", | |
" exec code\n", | |
" t1 = time.time()\n", | |
" ms = 0.5 * (ms + (t1-t0)*1000)\n", | |
" cnt += 1\n", | |
" return ms" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 13 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"meth = [m for m in dir() if m.startswith('method_')]\n", | |
"timing = {}\n", | |
"for m in meth:\n", | |
" timing[m] = _timeit(m+'()', repeat=10)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 14 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"sorted(timing.items(), key=lambda x: x[1])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 15, | |
"text": [ | |
"[('method_8_numpy', 0.3586830571293831),\n", | |
" ('method_6_list_compr', 3.315247595310211),\n", | |
" ('method_1_bt', 3.7367017939686775),\n", | |
" ('method_4_list', 4.171660635620356),\n", | |
" ('method_7_buffer', 5.43083855882287),\n", | |
" ('method_0_repr', 6.246051285415888),\n", | |
" ('method_3_array', 7.965344935655594),\n", | |
" ('method_0_str', 7.995434571057558),\n", | |
" ('method_5_cStr', 12.76054373010993)]" | |
] | |
} | |
], | |
"prompt_number": 15 | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment