Skip to content

Instantly share code, notes, and snippets.

@jakirkham
Forked from quasiben/cudf-iloc-timings.ipynb
Created August 21, 2019 23:50
Show Gist options
  • Save jakirkham/602ed7e119531ea063da253e86ac4ac4 to your computer and use it in GitHub Desktop.
Save jakirkham/602ed7e119531ea063da253e86ac4ac4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import cudf"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({'a': range(100000)})\n",
"arr = np.arange(40, 50000)\n",
"cdf = cudf.from_pandas(df)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"24.1 ms ± 1.57 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[arr] # vanilla numpy array"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.77 ms ± 17.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[slice(40, 50000)] # slicing"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"519 µs ± 4.14 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit sr = cudf.Series(arr)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"513 µs ± 8.24 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"%timeit cudf.Series(arr) # timing for device->host transfer"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"sr = cudf.Series(arr)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20.1 ms ± 2.44 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[sr] # gpu backed array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Indexing into cudf DataFrame wiith GPU and NumPy index run at nearly the same speed.**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Test With Shuffled Indicies"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"np.random.shuffle(arr)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 3130, 25506, 820, ..., 17052, 9296, 16724])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arr"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12.5 ms ± 760 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[arr] # shuffled numpy array"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0 3130\n",
"1 25506\n",
"2 820\n",
"3 31545\n",
"4 7816\n",
"dtype: int64"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sr = cudf.Series(arr)\n",
"sr.head()"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"14.6 ms ± 641 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[sr] # gpu backed array"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try with CuPy"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"import cupy\n",
"c_arr = cupy.asarray(arr)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13.1 ms ± 2.01 ms per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[c_arr]"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"%load_ext snakeviz"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Try with Integer Column name"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"df = pd.DataFrame({0: range(100000)})\n",
"arr = np.arange(40, 50000)\n",
"cdf = cudf.from_pandas(df)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13.9 ms ± 302 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit cdf.iloc[arr]"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" \n",
"*** Profile stats marshalled to file '/tmp/tmp5s87ym3m'. \n",
"Embedding SnakeViz in this document...\n"
]
},
{
"data": {
"text/html": [
"\n",
"<iframe id='snakeviz-9b960bd2-c42c-11e9-a06b-d8c49764f624' frameborder=0 seamless width='100%' height='1000'></iframe>\n",
"<script>document.getElementById(\"snakeviz-9b960bd2-c42c-11e9-a06b-d8c49764f624\").setAttribute(\"src\", \"http://\" + document.location.hostname + \":8080/snakeviz/%2Ftmp%2Ftmp5s87ym3m\")</script>\n"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%%snakeviz\n",
"cdf.iloc[arr]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:rapids-0.9]",
"language": "python",
"name": "conda-env-rapids-0.9-py"
},
"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": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment