Skip to content

Instantly share code, notes, and snippets.

@Liso77

Liso77/bb.ipynb Secret

Last active February 20, 2017 19:29
Show Gist options
  • Save Liso77/837630cc64bc258a2a7a to your computer and use it in GitHub Desktop.
Save Liso77/837630cc64bc258a2a7a to your computer and use it in GitHub Desktop.
aa
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "map_loop2"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"Map vs For Loop"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"I've been looking at a lot of mapping functions today and I thought I would see how maps fare against a simple for loop."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"test=[]\n",
"\n",
"def tst(func):\n",
" global test\n",
" test+=[[func, func.__name__]]\n",
" return func\n",
"\n",
"@tst\n",
"def convert_for1(data):\n",
" output = []\n",
" for line in data:\n",
" x,y = line.split(',')\n",
" output+=[[float(x),float(y)]]\n",
" return output\n",
"\n",
"@tst\n",
"def convert_for2(data):\n",
" output = []\n",
" for line in data:\n",
" x,y = line.split(',')\n",
" output.append([float(x),float(y)])\n",
" return output\n",
"\n",
"@tst\n",
"def convert_map(data):\n",
" return [list(map(float, x.split(','))) for x in data]\n",
"\n",
"@tst\n",
"def convert_map2(data):\n",
" return [[i for i in map(float,x.split(','))] for x in data]\n",
"\n",
"@tst\n",
"def convert_map3(data):\n",
" return [map(float, x.split(',')) for x in data]\n",
"\n",
"@tst\n",
"def convert_map4(data):\n",
" return map(lambda line:map(float, line.split(',')), data)\n",
"\n",
"@tst\n",
"def convert_map5(data):\n",
" return [[j for j in i] for i in map(lambda line:map(float, line.split(',')), data)]\n",
"\n",
"@tst\n",
"def convert_compr(data):\n",
" return [[float(i), float(j)] for i,j in [k.split(',') for k in data]]\n",
"\n",
"@tst\n",
"def convert_gener(data):\n",
" return [[float(i), float(j)] for i,j in (k.split(',') for k in data)]\n",
"\n",
"@tst\n",
"def convert_list_comprehension(data):\n",
" \"\"\"This is the same as convert_map, just faster and more readable\"\"\"\n",
" return [[float(y) for y in x.split(',')] for x in data]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 388
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well I have to admit that the code looks a *lot* cleaner using the mapping function."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"text = \"\"\" 1,2\n",
" 2,3\n",
" 3,4\n",
" 5,6\n",
" 7,8\"\"\"\n",
"data = text.splitlines()\n",
"data = ['0.1, 0.3', '0.4, 0.5', '0.7, 0.8', '0.3, 0.9', '0.9, 0.4', '1.0, 0.9', '0.0, 0.5', '0.1, 0.8', '0.9, 0.8', '0.8, 0.6']"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 389
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Checking to see that they return the same thing..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"result=convert_for1(data)\n",
"print(result)\n",
"for i in test:\n",
" print(i[1], i[0](data)==result)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[0.1, 0.3], [0.4, 0.5], [0.7, 0.8], [0.3, 0.9], [0.9, 0.4], [1.0, 0.9], [0.0, 0.5], [0.1, 0.8], [0.9, 0.8], [0.8, 0.6]]\n",
"('convert_for1', True)\n",
"('convert_for2', True)\n",
"('convert_map', True)\n",
"('convert_map2', True)\n",
"('convert_map3', True)\n",
"('convert_map4', True)\n",
"('convert_map5', True)\n",
"('convert_compr', True)\n",
"('convert_gener', True)\n",
"('convert_list_comprehension', True)\n"
]
}
],
"prompt_number": 390
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let check on the speed now. Does few lines of code mean a faster function?"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import markdown\n",
"class MD(str):\n",
" def _repr_html_(self):\n",
" return markdown.markdown(self)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 392
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for i in test:\n",
" print(i[1])\n",
" %timeit i[0](data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"convert_for1\n",
"100000 loops, best of 3: 7.7 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_for2\n",
"100000 loops, best of 3: 7.24 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_map\n",
"100000 loops, best of 3: 9.55 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_map2\n",
"100000 loops, best of 3: 8.95 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_map3\n",
"100000 loops, best of 3: 7.47 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_map4\n",
"100000 loops, best of 3: 8.33 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_map5\n",
"100000 loops, best of 3: 10.5 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_compr\n",
"100000 loops, best of 3: 7.04 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_gener\n",
"100000 loops, best of 3: 7.19 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"convert_list_comprehension\n",
"100000 loops, best of 3: 7.12 us per loop"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n"
]
}
],
"prompt_number": 393
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment