Skip to content

Instantly share code, notes, and snippets.

@rutj3
Last active July 12, 2017 09:31
Show Gist options
  • Save rutj3/57297c37cd6a62349e789bc23c8f04ec to your computer and use it in GitHub Desktop.
Save rutj3/57297c37cd6a62349e789bc23c8f04ec to your computer and use it in GitHub Desktop.
Numba_median
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import numba\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1.13.1'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.__version__"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'0.34.0'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"numba.__version__"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def numpy_median(x): \n",
" for i in range(x.size):\n",
" np.median(x)\n",
" \n",
"@numba.njit\n",
"def numba_median(x): \n",
" for i in range(x.size):\n",
" np.median(x)\n",
" \n",
"@numba.njit\n",
"def custom_median(x):\n",
" \n",
" n = x.size \n",
" xsort = np.sort(x)\n",
" \n",
" if n % 2 == 0:\n",
" # even\n",
" return (xsort[int(n//2)-1] + xsort[int(n//2)]) / 2\n",
" else:\n",
" # odd\n",
" return xsort[int(n//2)]\n",
" \n",
"@numba.njit\n",
"def numba_median_custom(x): \n",
" for i in range(x.size):\n",
" custom_median(x)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(dtype('float64'), dtype('float64'))"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n = 1500\n",
"\n",
"x_equal = np.zeros(n)\n",
"x_random = np.random.randn(n)\n",
"\n",
"x_equal.dtype, x_random.dtype"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Numpy"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"89.8 ms ± 2.62 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"np_eq = %timeit -o numpy_median(x_equal)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"95.4 ms ± 1.11 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"np_ra = %timeit -o numpy_median(x_random)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Numba"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.03 s ± 36.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"nb_eq = %timeit -o numba_median(x_equal)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"16.9 ms ± 303 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"nb_ra = %timeit -o numba_median(x_random)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Numba custom median"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"61.9 ms ± 4.57 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"nb_cust_eq = %timeit -o numba_median_custom(x_equal)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"136 ms ± 2.61 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"nb_cust_ra = %timeit -o numba_median_custom(x_random)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"181.17470071328444"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# numba (np.median)\n",
"nb_eq.best / nb_ra.best"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.43186158466387026"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# numba (custom median)\n",
"nb_cust_eq.best / nb_cust_ra.best"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment