Skip to content

Instantly share code, notes, and snippets.

@mamrehn
Created January 12, 2017 15:23
Show Gist options
  • Save mamrehn/0f90fad6cf1c84dd621e1947d808e07f to your computer and use it in GitHub Desktop.
Save mamrehn/0f90fad6cf1c84dd621e1947d808e07f to your computer and use it in GitHub Desktop.
Test for StackOverflow question about python's list comprehension vs.map. http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Python List Comprehension Vs. Map\n",
"\n",
"Soure: [stackoverflow.com](http://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"= Last run on 2017-01-12 =\n",
"Windows-7-6.1.7601-SP1, Intel64 Family 6 Model 60 Stepping 3, GenuineIntel, 4 CPUs\n",
"\n",
"CPython:\t3.5.2 (MSC v.1900 64 bit (AMD64))\n"
]
}
],
"source": [
"import platform as p\n",
"import multiprocessing as mp\n",
"import time; print('= Last run on {0} =\\n{1}, {7}, {8} CPUs\\n'.format(time.strftime('%Y-%m-%d'), p.platform(), *p.uname(), mp.cpu_count() >> 1))\n",
"import sys; print('{}:\\t{} ({})'.format(p.python_implementation(), '.'.join(p.python_version_tuple()), p.python_compiler()))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary:\n",
"\n",
"For non-lambda functions, using *map* is (still) slightly faster than *list comprehension*.<br>\n",
"For lambda functions, use *list comprehensions*.<br>\n",
"Use `tuple(map( ... ))` instead of `list(map( ... ))` for another slight boost in performance.<br><br>\n",
"Note: Tested only on *very* small data sets!"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Warm Up"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"xs = range(100)\n",
"r = [tuple(map(str, xs)) for _ in range(100)]\n",
"del r"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Original Test by Alex Martelli"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"max_num = 10"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 5.25 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 1.38 µs per loop\n"
]
}
],
"source": [
"xs = range(max_num)\n",
"%timeit tuple(map(hex, xs))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000000 loops, best of 3: 1.44 µs per loop\n"
]
}
],
"source": [
"xs = range(max_num)\n",
"%timeit list(map(hex, xs))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000000 loops, best of 3: 1.52 µs per loop\n"
]
}
],
"source": [
"xs = range(max_num)\n",
"%timeit [hex(x) for x in xs]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**=> here, map is slightly faster than list comprehensions**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The slowest run took 4.18 times longer than the fastest. This could mean that an intermediate result is being cached.\n",
"1000000 loops, best of 3: 1.54 µs per loop\n"
]
}
],
"source": [
"xs = range(max_num)\n",
"%timeit tuple(map(lambda x: x + 2, xs))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000000 loops, best of 3: 1.59 µs per loop\n"
]
}
],
"source": [
"xs = range(max_num)\n",
"%timeit list(map(lambda x: x + 2, xs))"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1000000 loops, best of 3: 786 ns per loop\n"
]
}
],
"source": [
"xs = range(max_num)\n",
"%timeit [x + 2 for x in xs]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**=> here, list comprehensions are faster than map**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Attila Szlovencsák's Test"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"max_num *= 10"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000 loops, best of 3: 36.2 µs per loop\n"
]
}
],
"source": [
"s = 'abcd' * max_num\n",
"%timeit [x.upper() for x in s if x < 'c']"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10000 loops, best of 3: 77.9 µs per loop\n"
]
}
],
"source": [
"s = 'abcd' * max_num\n",
"%timeit tuple(map(str.upper,filter(lambda x:x < 'c', s)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**=> here, list comprehensions are faster than map**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Karl Knechtel's Test"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100000 loops, best of 3: 11.6 µs per loop\n"
]
}
],
"source": [
"def test(x):\n",
" return x + 1\n",
"\n",
"%timeit [test(i) for i in range(max_num)]"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100000 loops, best of 3: 12.1 µs per loop\n"
]
}
],
"source": [
"%timeit tuple(map(test, range(max_num)))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100000 loops, best of 3: 11.3 µs per loop\n"
]
}
],
"source": [
"%timeit [lambda x: x + 1 for i in range(max_num)]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100000 loops, best of 3: 12.2 µs per loop\n"
]
}
],
"source": [
"%timeit tuple(map(lambda x: x + 1, range(max_num)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**=> here, for non-lambda functions, map is slightly faster than list comprehensions.\n",
"Else, list comprehensions are faster than map**"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment