Skip to content

Instantly share code, notes, and snippets.

@aldanor
Created June 22, 2019 14:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aldanor/211cca81247f00b241838d807de114a0 to your computer and use it in GitHub Desktop.
Save aldanor/211cca81247f00b241838d807de114a0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import numba as nb\n",
"import numexpr as ne"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"np.random.seed(0)\n",
"data = np.random.randint(0, 100, (10_000_000, 10))\n",
"columns = list('abcdefghij')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Plain Python"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.46 s ± 40.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit sum(row[2] for row in data if row[0] > 50 and row[1] > 50)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Pandas DataFrame"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame(data, columns=columns)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"388 ms ± 4.27 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit df[(df['a'] > 50) & (df['b'] > 50)]['c'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"368 ms ± 10.4 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit df.query('a > 50 and b > 50')['c'].sum()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"248 ms ± 3.09 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit df['c'][(df['a'] > 50) & (df['b'] > 50)].sum()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"229 ms ± 4.24 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit df['c'][df.eval('a > 50 and b > 50')].sum()"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"163 ms ± 2.79 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit df['c'].values[df.eval('a > 50 and b > 50').values].sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Record Arrays / NumExpr"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"rec = np.rec.fromarrays(data.T, names=columns)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"170 ms ± 955 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit rec['c'][(rec['a'] > 50) & (rec['b'] > 50)].sum()"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"114 ms ± 1.14 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit ne.evaluate('sum(where((a > 50) & (b > 50), c, 0))', df)"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"114 ms ± 1.35 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit ne.evaluate('sum(where((a > 50) & (b > 50), c, 0))', rec)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Numba"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [],
"source": [
"@nb.njit\n",
"def do_it(rec):\n",
" s = 0\n",
" for r in rec:\n",
" if r.a > 50 and r.b > 50:\n",
" s += r.c\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"75.4 ms ± 357 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit do_it(rec)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"@nb.njit(parallel=True)\n",
"def do_it_parallel(rec):\n",
" s = 0\n",
" for i in nb.prange(len(rec)):\n",
" r = rec[i]\n",
" if r.a > 50 and r.b > 50:\n",
" s += r.c\n",
" return s"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"41.6 ms ± 3.06 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit do_it_parallel(rec)"
]
}
],
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment