Skip to content

Instantly share code, notes, and snippets.

@ankostis
Last active July 30, 2020 21:05
Show Gist options
  • Save ankostis/c5db9e5f5b3317511fa742ad73246486 to your computer and use it in GitHub Desktop.
Save ankostis/c5db9e5f5b3317511fa742ad73246486 to your computer and use it in GitHub Desktop.
Compare List-comprehensions with Nested-loops
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Compare one-liner comprehension summing numbers with a nested loop version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Build the sample *dataset*:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"from math import sqrt\n",
"from random import sample\n",
"\n",
"population_size = int(10e10)\n",
"population = range(population_size)\n",
"def square_lists(size):\n",
" return [list(sample(population, k=size)) for i in range(size)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define the test functions:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [],
"source": [
"def sum_items_loops(items):\n",
" size = len(items)\n",
" s = 0\n",
" for i in range(size):\n",
" for j in range(size):\n",
" s += items[i][j]\n",
" return s\n",
"\n",
"def sum_items_comprehension(items):\n",
" return sum(ii for i in items for ii in i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## With a small dataset the one-liner is x2 faster:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.11 µs ± 62.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
]
}
],
"source": [
"items = square_lists(6) # 36 integers\n",
"\n",
"%timeit sum_items_loops(items)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.73 µs ± 7.94 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)\n"
]
}
],
"source": [
"%timeit sum_items_comprehension(items)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ... and remains the same with a bigger dataset:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.12 s ± 13.1 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"items = square_lists(2<<11) # 16_777_216, 16Mi integers\n",
"\n",
"%timeit sum_items_loops(items)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"jupyter": {
"outputs_hidden": false
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"551 ms ± 2.99 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit sum_items_comprehension(items)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## It's interesting that numpy is *x90* faster:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"\n",
"items = np.array(items)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12.4 ms ± 130 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit np.sum(items)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## To Panos: would be nice to compare with a GPU version..."
]
}
],
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment