Skip to content

Instantly share code, notes, and snippets.

@wesm
Created August 15, 2019 16:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesm/b9e917b46536676d2ed829bc4458ba3d to your computer and use it in GitHub Desktop.
Save wesm/b9e917b46536676d2ed829bc4458ba3d to your computer and use it in GitHub Desktop.
ARROW-3246 Benchmarks
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 pyarrow as pa\n",
"import pyarrow.parquet as pq\n",
"import pandas as pd\n",
"from pandas.util.testing import rands\n",
" \n",
"NUNIQUE = 1000\n",
"STRING_SIZE = 50\n",
"LENGTH = 10_000_000\n",
"REPEATS = LENGTH // NUNIQUE\n",
"\n",
"uniques = np.array([rands(STRING_SIZE) for i in range(NUNIQUE)], dtype='O')\n",
"indices = np.random.randint(0, NUNIQUE, size=LENGTH).astype('i4') \n",
"data = uniques.take(indices)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import gc\n",
"class memory_use:\n",
" \n",
" def __init__(self):\n",
" self.start_use = pa.total_allocated_bytes() \n",
" self.pool = pa.default_memory_pool()\n",
" self.start_peak_use = self.pool.max_memory()\n",
" \n",
" def __enter__(self):\n",
" return\n",
" \n",
" def __exit__(self, type, value, traceback):\n",
" gc.collect()\n",
" print(\"Change in memory use: {}\"\n",
" .format(pa.total_allocated_bytes() - self.start_use))\n",
" print(\"Change in peak use: {}\"\n",
" .format(self.pool.max_memory() - self.start_peak_use))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"dict_data = pa.DictionaryArray.from_arrays(indices, uniques)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"72320"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pa.default_memory_pool().max_memory()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Change in memory use: 16777216\n",
"Change in peak use: 753475648\n"
]
}
],
"source": [
"table = pa.table([dict_data], names=['f0'])\n",
"with memory_use():\n",
" out_stream = pa.BufferOutputStream()\n",
" pq.write_table(table, out_stream)\n",
" contents = out_stream.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"820 ms ± 11.3 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"out_stream = pa.BufferOutputStream()\n",
"pq.write_table(table, out_stream)\n",
"contents = out_stream.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12576182"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(contents)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"495 ms ± 8.04 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit returned_table = pq.read_table(contents)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"93.1 ms ± 3.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit returned_table = pq.read_table(contents, read_dictionary=['f0'])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"dense_data = dict_data.cast(pa.utf8())\n",
"table = pa.table([dense_data], names=['f0'])"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"405 ms ± 27.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"out_stream = pa.BufferOutputStream()\n",
"pq.write_table(table, out_stream)\n",
"contents = out_stream.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"out_stream = pa.BufferOutputStream()\n",
"pq.write_table(table, out_stream)\n",
"contents = out_stream.getvalue()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"430 ms ± 8.12 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%%timeit\n",
"returned_table = pq.read_table(contents)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"pyarrow.Table\n",
"f0: string"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pq.read_table(contents)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.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