Skip to content

Instantly share code, notes, and snippets.

@agoose77
Created June 9, 2021 15:25
Show Gist options
  • Save agoose77/90d13a395af9f61f80328ed041cfa789 to your computer and use it in GitHub Desktop.
Save agoose77/90d13a395af9f61f80328ed041cfa789 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,
"id": "90db6855-6a68-4852-85df-0ca08b010895",
"metadata": {},
"outputs": [],
"source": [
"import awkward as ak\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"id": "5a7fcfcd-4e89-4501-8a38-68451d1a673b",
"metadata": {},
"source": [
"## Define lookup table"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "c3e58475-fbba-4689-8b82-a7167216385d",
"metadata": {},
"outputs": [],
"source": [
"lookup = ak.zip(\n",
" {\n",
" \"x\": np.random.random(size=(4, 4, 64)),\n",
" \"y\": np.random.random(size=(4, 4, 64)),\n",
" }\n",
")\n",
"lookup_var = lookup\n",
"for i in range(3):\n",
" lookup = ak.to_regular(lookup, axis=i)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "547e7dda-18da-4e13-8555-2be52df18005",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4 * 4 * 64 * {\"x\": float64, \"y\": float64}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lookup.type"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "a0ce8216-6717-42b5-90ed-94d69bcadd2d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4 * var * var * {\"x\": float64, \"y\": float64}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"lookup_var.type"
]
},
{
"cell_type": "markdown",
"id": "d20c45e2-e6dc-4f41-912c-8a93f52d8623",
"metadata": {},
"source": [
"## Define indices"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0291c174-7cae-4cf9-8fd8-95036c1ffedb",
"metadata": {},
"outputs": [],
"source": [
"u, v, w = np.ascontiguousarray(\n",
" np.random.randint([0, 0, 0], [4, 4, 64], size=(100_000, 3)).T\n",
")"
]
},
{
"cell_type": "markdown",
"id": "f5d03509-ebdf-415c-823a-a85e4e965d48",
"metadata": {},
"source": [
"## Record benchmarks"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "ad4bb663-7a3c-4092-b65e-4b79258f85bb",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"128 ms ± 2.12 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%timeit lookup[u, v, w]"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "a96ea862-fbad-4e93-973a-e0211c5564dc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.04 ms ± 95.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit lookup_var[u, v, w]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "7595156d-d096-48f7-aa3a-c4360f5afcdd",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.45 ms ± 85.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"lookup_np = ak.to_numpy(lookup)\n",
"%timeit lookup_np[u, v, w]"
]
},
{
"cell_type": "markdown",
"id": "03a53da5-f03c-4f00-8497-032e9838df5d",
"metadata": {},
"source": [
"## Field"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "fb2be723-0ae1-4689-9d7a-ac28a0509a09",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"278 ms ± 3.07 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
]
}
],
"source": [
"%timeit lookup.x[u, v, w]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "e510ca30-9cb8-42c7-9304-ce57c109f471",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.36 ms ± 61.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%timeit lookup_var.x[u, v, w]"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "c359ed9c-f23d-44ae-8eaa-c94a33ddbb36",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"551 µs ± 4.47 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
]
}
],
"source": [
"lookup_x_np = ak.to_numpy(lookup.x)\n",
"%timeit lookup_x_np[u, v, w]"
]
}
],
"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.9.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
awkward
numpy
jupyterlab
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment