Skip to content

Instantly share code, notes, and snippets.

Created July 4, 2013 04:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5924999 to your computer and use it in GitHub Desktop.
Save anonymous/5924999 to your computer and use it in GitHub Desktop.
Mapping vs for loop in a speed contest.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "Map vs for loop"
},
"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": [
"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",
"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",
"def convert_map(data):\n",
" return [list(map(float, x.split(','))) for x in data]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"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",
"data = text.splitlines()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Checking to see that they return the same thing..."
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"convert_for1(data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 3,
"text": [
"[[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"convert_for2(data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 4,
"text": [
"[[[1.0, 2.0]], [[2.0, 3.0]], [[3.0, 4.0]]]"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"convert_map(data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 5,
"text": [
"[[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]"
]
}
],
"prompt_number": 5
},
{
"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": [
"%timeit convert_for1(data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 5.63 us per loop\n"
]
}
],
"prompt_number": 6
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit convert_for2(data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 5.66 us per loop\n"
]
}
],
"prompt_number": 7
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"%timeit convert_map(data)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"100000 loops, best of 3: 8.79 us per loop\n"
]
}
],
"prompt_number": 8
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well I guess that settles it for me. I find the convert_for1 loop the most readable, and now I see that it is just faster too now. That is the approach I'm using from now on.\n",
"\n",
"Oh, I know that using numba or cython would be faster, but this is the approach I'll use unless I am looking for real speed."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment