Skip to content

Commit

Permalink
API timings
Browse files Browse the repository at this point in the history
  • Loading branch information
ajfriend committed Sep 3, 2021
1 parent 2088cfb commit 90c688c
Show file tree
Hide file tree
Showing 2 changed files with 334 additions and 2 deletions.
5 changes: 3 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
init: purge
virtualenv -p python3 env
python -m venv env
env/bin/pip install --upgrade pip wheel setuptools
env/bin/pip install -r requirements.txt

purge:
Expand All @@ -9,4 +10,4 @@ lab:
env/bin/jupyter lab

ipython:
env/bin/ipython
env/bin/ipython
331 changes: 331 additions & 0 deletions notebooks/time_h3_apis.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,331 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "95776ece-9628-40ad-a6cb-bf68690167ec",
"metadata": {},
"source": [
"# API Speed Comparison\n",
"\n",
"We time a simple toy computational task across the various APIs.\n",
"We also compare the option of doing most of the computation in one of the faster APIs (using the `int` representation of H3 indexes),\n",
"and then converting the results to the more familiar format of Python `str` objects."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "cef5ba28-c8b0-42de-b55f-7a3d92436b32",
"metadata": {},
"outputs": [],
"source": [
"import h3\n",
"import h3.api.numpy_int\n",
"from time import sleep\n",
"\n",
"sleep_seconds = 20\n",
"\n",
"def compute(h3_lib, N=100):\n",
" h = h3_lib.geo_to_h3(0, 0, 9)\n",
" out = h3_lib.k_ring(h, N)\n",
" out = h3_lib.compact(out)\n",
" \n",
" return out\n",
"\n",
"def compute_and_convert(h3_lib, N=100):\n",
" out = [\n",
" h3.h3_to_string(h)\n",
" for h in compute(h3_lib, N)\n",
" ]\n",
" \n",
" return out"
]
},
{
"cell_type": "markdown",
"id": "9c933e85-5a9a-4d07-b4c4-20a88c97fa88",
"metadata": {},
"source": [
"# Compute with each API\n",
"\n",
"**Benchmarking note**: We put a `sleep()` before each timing to help stablize the results (at least on my laptop)."
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "1889e459-0482-44f8-a4a9-a363f6a64231",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e143ba5e-618b-47a9-913b-66f25877b240",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"54.2 ms ± 643 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute(h3.api.basic_str)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "e4b4f37e-9c23-408a-84e0-ee8a4e92d793",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6d80bca9-c0f5-4aaa-8488-f7bd174c7ee3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"31.1 ms ± 372 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute(h3.api.basic_int)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "766baaec-a40f-4a5c-8d9f-3c48c2835697",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "f2713e84-c4fb-4a60-b913-c1fb7ea7fd4b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.69 ms ± 75.5 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute(h3.api.numpy_int)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "fa9186bb-1ffe-48be-99c3-f08f9aae2ce2",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "e9931ac6-eeba-4168-92e1-f8f330b1ca42",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"6.67 ms ± 72.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute(h3.api.memview_int)"
]
},
{
"cell_type": "markdown",
"id": "97e8e885-401b-42aa-a28e-534f828bf003",
"metadata": {},
"source": [
"# Compute with `int` APIs and convert to `str`"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "ea7e8528-d84d-4e23-af12-9daab077aea7",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "51c02c9f-0a03-4e84-943d-09969949b83d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"34 ms ± 421 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute_and_convert(h3.api.basic_int)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "d56ba926-9c5e-49d2-9fb6-b897070cc474",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "b11e3d6d-58d3-418f-9798-2a80dc16b133",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.85 ms ± 99.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute_and_convert(h3.api.memview_int)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "eab6f2d9-f52a-44e7-9079-19d1754c5a20",
"metadata": {},
"outputs": [],
"source": [
"sleep(sleep_seconds)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "5cf6e8a8-583d-4309-8ed2-5b4c31c8765a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"7.83 ms ± 101 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
]
}
],
"source": [
"%%timeit\n",
"compute_and_convert(h3.api.numpy_int)"
]
},
{
"cell_type": "markdown",
"id": "381e7c85-a70a-4563-8504-29e39639821f",
"metadata": {},
"source": [
"# Speedup\n",
"\n",
"We typically see about a 6--8x speedup, comparing the `h3.api.basic_str` interface against computing with the `h3.api.numpy_int` and then converting the results back to `str`."
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "188da0e8-efd8-43f5-9f45-b821e085b8a7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6.922094508301405"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# REMEMBER! update the numbers from the `%%timeit`s above!\n",
"\n",
"c_str = 54.2\n",
"cnc_numpy = 7.83\n",
"\n",
"c_str/cnc_numpy"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "233d4a44-f8e6-42d2-b54d-b6001c776102",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 90c688c

Please sign in to comment.