Skip to content

Instantly share code, notes, and snippets.

@capttwinky
Last active September 29, 2015 21:38
Show Gist options
  • Save capttwinky/6929532d9eb9f87990f3 to your computer and use it in GitHub Desktop.
Save capttwinky/6929532d9eb9f87990f3 to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:093e4d7561eebe0a00236a799d36efc6bdc0dd00e7517505b7ab14026324aef7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import random\n",
"import string\n",
"from dis import dis\n",
"\n",
"def funa(app_id):\n",
" parts = app_id.split('com.tinycorp.')\n",
" if parts[0] == '' and len(parts) > 1:\n",
" return parts[1]\n",
" else:\n",
" return app_id\n",
"\n",
"def funb(str_in, preamble=None):\n",
" return str_in if not str_in.startswith(preamble) else str_in[len(preamble):]\n",
"\n",
"mfn_list = ['{}{}'.format('com.tinycorp.' if random.randint(0,1) else '', ''.join(random.sample(string.letters, 5))) for i in xrange(10000)]\n",
"\n",
"print \"first version run time\"\n",
"%time _ = [funa(ml) for ml in mfn_list]\n",
"print \"second version run time\"\n",
"%time _ = [funb(ml, 'com.tinycorp.') for ml in mfn_list]\n",
"\n",
"print \"function a disassembled\"\n",
"dis(funa)\n",
"\n",
"print \"function b disassembled\"\n",
"dis(funb)\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"first version run time\n",
"CPU times: user 4.42 ms, sys: 0 ns, total: 4.42 ms\n",
"Wall time: 4.44 ms\n",
"second version run time\n",
"CPU times: user 4.01 ms, sys: 0 ns, total: 4.01 ms\n",
"Wall time: 3.95 ms\n",
"function a disassembled\n",
" 6 0 LOAD_FAST 0 (app_id)\n",
" 3 LOAD_ATTR 0 (split)\n",
" 6 LOAD_CONST 1 ('com.tinycorp.')\n",
" 9 CALL_FUNCTION 1\n",
" 12 STORE_FAST 1 (parts)\n",
"\n",
" 7 15 LOAD_FAST 1 (parts)\n",
" 18 LOAD_CONST 2 (0)\n",
" 21 BINARY_SUBSCR \n",
" 22 LOAD_CONST 3 ('')\n",
" 25 COMPARE_OP 2 (==)\n",
" 28 POP_JUMP_IF_FALSE 57\n",
" 31 LOAD_GLOBAL 1 (len)\n",
" 34 LOAD_FAST 1 (parts)\n",
" 37 CALL_FUNCTION 1\n",
" 40 LOAD_CONST 4 (1)\n",
" 43 COMPARE_OP 4 (>)\n",
" 46 POP_JUMP_IF_FALSE 57\n",
"\n",
" 8 49 LOAD_FAST 1 (parts)\n",
" 52 LOAD_CONST 4 (1)\n",
" 55 BINARY_SUBSCR \n",
" 56 RETURN_VALUE \n",
"\n",
" 10 >> 57 LOAD_FAST 0 (app_id)\n",
" 60 RETURN_VALUE \n",
" 61 LOAD_CONST 0 (None)\n",
" 64 RETURN_VALUE \n",
"function b disassembled\n",
" 13 0 LOAD_FAST 0 (str_in)\n",
" 3 LOAD_ATTR 0 (startswith)\n",
" 6 LOAD_FAST 1 (preamble)\n",
" 9 CALL_FUNCTION 1\n",
" 12 POP_JUMP_IF_TRUE 19\n",
" 15 LOAD_FAST 0 (str_in)\n",
" 18 RETURN_VALUE \n",
" >> 19 LOAD_FAST 0 (str_in)\n",
" 22 LOAD_GLOBAL 1 (len)\n",
" 25 LOAD_FAST 1 (preamble)\n",
" 28 CALL_FUNCTION 1\n",
" 31 SLICE+1 \n",
" 32 RETURN_VALUE \n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment