|
8 | 8 | "# API Speed Comparison\n", |
9 | 9 | "\n", |
10 | 10 | "We time a simple toy computational task across the various APIs.\n", |
| 11 | + "\n", |
11 | 12 | "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", |
12 | 13 | "and then converting the results to the more familiar format of Python `str` objects." |
13 | 14 | ] |
|
21 | 22 | "source": [ |
22 | 23 | "import h3\n", |
23 | 24 | "import h3.api.numpy_int\n", |
24 | | - "from time import sleep\n", |
25 | 25 | "\n", |
26 | | - "sleep_seconds = 20\n", |
27 | 26 | "\n", |
28 | 27 | "def compute(h3_lib, N=100):\n", |
29 | 28 | " h = h3_lib.geo_to_h3(0, 0, 9)\n", |
|
48 | 47 | "source": [ |
49 | 48 | "# Compute with each API\n", |
50 | 49 | "\n", |
51 | | - "**Benchmarking note**: We put a `sleep()` before each timing to help stablize the results (at least on my laptop)." |
| 50 | + "**Benchmarking note**: `%%timeit sleep()` executes `sleep` before each **run** of the code to be timed in the remaining body of the cell; this helps to stabilize timing results (at least on my laptop)." |
52 | 51 | ] |
53 | 52 | }, |
54 | 53 | { |
55 | 54 | "cell_type": "code", |
56 | 55 | "execution_count": 2, |
57 | | - "id": "1889e459-0482-44f8-a4a9-a363f6a64231", |
| 56 | + "id": "e636e193-22ff-440f-a5b8-4c073c8f8a57", |
58 | 57 | "metadata": {}, |
59 | 58 | "outputs": [], |
60 | 59 | "source": [ |
61 | | - "sleep(sleep_seconds)" |
| 60 | + "def sleep():\n", |
| 61 | + " import time\n", |
| 62 | + " time.sleep(2)" |
62 | 63 | ] |
63 | 64 | }, |
64 | 65 | { |
|
71 | 72 | "name": "stdout", |
72 | 73 | "output_type": "stream", |
73 | 74 | "text": [ |
74 | | - "54.2 ms ± 643 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" |
| 75 | + "58.4 ms ± 876 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" |
75 | 76 | ] |
76 | 77 | } |
77 | 78 | ], |
78 | 79 | "source": [ |
79 | | - "%%timeit\n", |
| 80 | + "%%timeit sleep()\n", |
| 81 | + "\n", |
80 | 82 | "compute(h3.api.basic_str)" |
81 | 83 | ] |
82 | 84 | }, |
83 | 85 | { |
84 | 86 | "cell_type": "code", |
85 | 87 | "execution_count": 4, |
86 | | - "id": "e4b4f37e-9c23-408a-84e0-ee8a4e92d793", |
87 | | - "metadata": {}, |
88 | | - "outputs": [], |
89 | | - "source": [ |
90 | | - "sleep(sleep_seconds)" |
91 | | - ] |
92 | | - }, |
93 | | - { |
94 | | - "cell_type": "code", |
95 | | - "execution_count": 5, |
96 | 88 | "id": "6d80bca9-c0f5-4aaa-8488-f7bd174c7ee3", |
97 | 89 | "metadata": {}, |
98 | 90 | "outputs": [ |
99 | 91 | { |
100 | 92 | "name": "stdout", |
101 | 93 | "output_type": "stream", |
102 | 94 | "text": [ |
103 | | - "31.1 ms ± 372 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" |
| 95 | + "34.9 ms ± 910 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" |
104 | 96 | ] |
105 | 97 | } |
106 | 98 | ], |
107 | 99 | "source": [ |
108 | | - "%%timeit\n", |
| 100 | + "%%timeit sleep()\n", |
| 101 | + "\n", |
109 | 102 | "compute(h3.api.basic_int)" |
110 | 103 | ] |
111 | 104 | }, |
112 | 105 | { |
113 | 106 | "cell_type": "code", |
114 | | - "execution_count": 6, |
115 | | - "id": "766baaec-a40f-4a5c-8d9f-3c48c2835697", |
116 | | - "metadata": {}, |
117 | | - "outputs": [], |
118 | | - "source": [ |
119 | | - "sleep(sleep_seconds)" |
120 | | - ] |
121 | | - }, |
122 | | - { |
123 | | - "cell_type": "code", |
124 | | - "execution_count": 7, |
| 107 | + "execution_count": 5, |
125 | 108 | "id": "f2713e84-c4fb-4a60-b913-c1fb7ea7fd4b", |
126 | 109 | "metadata": {}, |
127 | 110 | "outputs": [ |
128 | 111 | { |
129 | 112 | "name": "stdout", |
130 | 113 | "output_type": "stream", |
131 | 114 | "text": [ |
132 | | - "6.69 ms ± 75.5 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
| 115 | + "6.96 ms ± 20.7 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
133 | 116 | ] |
134 | 117 | } |
135 | 118 | ], |
136 | 119 | "source": [ |
137 | | - "%%timeit\n", |
| 120 | + "%%timeit sleep()\n", |
| 121 | + "\n", |
138 | 122 | "compute(h3.api.numpy_int)" |
139 | 123 | ] |
140 | 124 | }, |
141 | 125 | { |
142 | 126 | "cell_type": "code", |
143 | | - "execution_count": 8, |
144 | | - "id": "fa9186bb-1ffe-48be-99c3-f08f9aae2ce2", |
145 | | - "metadata": {}, |
146 | | - "outputs": [], |
147 | | - "source": [ |
148 | | - "sleep(sleep_seconds)" |
149 | | - ] |
150 | | - }, |
151 | | - { |
152 | | - "cell_type": "code", |
153 | | - "execution_count": 9, |
| 127 | + "execution_count": 6, |
154 | 128 | "id": "e9931ac6-eeba-4168-92e1-f8f330b1ca42", |
155 | 129 | "metadata": {}, |
156 | 130 | "outputs": [ |
157 | 131 | { |
158 | 132 | "name": "stdout", |
159 | 133 | "output_type": "stream", |
160 | 134 | "text": [ |
161 | | - "6.67 ms ± 72.8 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
| 135 | + "7 ms ± 122 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
162 | 136 | ] |
163 | 137 | } |
164 | 138 | ], |
165 | 139 | "source": [ |
166 | | - "%%timeit\n", |
| 140 | + "%%timeit sleep()\n", |
| 141 | + "\n", |
167 | 142 | "compute(h3.api.memview_int)" |
168 | 143 | ] |
169 | 144 | }, |
|
177 | 152 | }, |
178 | 153 | { |
179 | 154 | "cell_type": "code", |
180 | | - "execution_count": 10, |
181 | | - "id": "ea7e8528-d84d-4e23-af12-9daab077aea7", |
182 | | - "metadata": {}, |
183 | | - "outputs": [], |
184 | | - "source": [ |
185 | | - "sleep(sleep_seconds)" |
186 | | - ] |
187 | | - }, |
188 | | - { |
189 | | - "cell_type": "code", |
190 | | - "execution_count": 11, |
| 155 | + "execution_count": 7, |
191 | 156 | "id": "51c02c9f-0a03-4e84-943d-09969949b83d", |
192 | 157 | "metadata": {}, |
193 | 158 | "outputs": [ |
194 | 159 | { |
195 | 160 | "name": "stdout", |
196 | 161 | "output_type": "stream", |
197 | 162 | "text": [ |
198 | | - "34 ms ± 421 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" |
| 163 | + "35.1 ms ± 360 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)\n" |
199 | 164 | ] |
200 | 165 | } |
201 | 166 | ], |
202 | 167 | "source": [ |
203 | | - "%%timeit\n", |
| 168 | + "%%timeit sleep()\n", |
| 169 | + "\n", |
204 | 170 | "compute_and_convert(h3.api.basic_int)" |
205 | 171 | ] |
206 | 172 | }, |
207 | 173 | { |
208 | 174 | "cell_type": "code", |
209 | | - "execution_count": 12, |
210 | | - "id": "d56ba926-9c5e-49d2-9fb6-b897070cc474", |
211 | | - "metadata": {}, |
212 | | - "outputs": [], |
213 | | - "source": [ |
214 | | - "sleep(sleep_seconds)" |
215 | | - ] |
216 | | - }, |
217 | | - { |
218 | | - "cell_type": "code", |
219 | | - "execution_count": 13, |
| 175 | + "execution_count": 8, |
220 | 176 | "id": "b11e3d6d-58d3-418f-9798-2a80dc16b133", |
221 | 177 | "metadata": {}, |
222 | 178 | "outputs": [ |
223 | 179 | { |
224 | 180 | "name": "stdout", |
225 | 181 | "output_type": "stream", |
226 | 182 | "text": [ |
227 | | - "7.85 ms ± 99.1 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
| 183 | + "7.61 ms ± 47 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
228 | 184 | ] |
229 | 185 | } |
230 | 186 | ], |
231 | 187 | "source": [ |
232 | | - "%%timeit\n", |
| 188 | + "%%timeit sleep()\n", |
| 189 | + "\n", |
233 | 190 | "compute_and_convert(h3.api.memview_int)" |
234 | 191 | ] |
235 | 192 | }, |
236 | 193 | { |
237 | 194 | "cell_type": "code", |
238 | | - "execution_count": 14, |
239 | | - "id": "eab6f2d9-f52a-44e7-9079-19d1754c5a20", |
240 | | - "metadata": {}, |
241 | | - "outputs": [], |
242 | | - "source": [ |
243 | | - "sleep(sleep_seconds)" |
244 | | - ] |
245 | | - }, |
246 | | - { |
247 | | - "cell_type": "code", |
248 | | - "execution_count": 15, |
| 195 | + "execution_count": 9, |
249 | 196 | "id": "5cf6e8a8-583d-4309-8ed2-5b4c31c8765a", |
250 | 197 | "metadata": {}, |
251 | 198 | "outputs": [ |
252 | 199 | { |
253 | 200 | "name": "stdout", |
254 | 201 | "output_type": "stream", |
255 | 202 | "text": [ |
256 | | - "7.83 ms ± 101 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
| 203 | + "7.69 ms ± 172 μs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" |
257 | 204 | ] |
258 | 205 | } |
259 | 206 | ], |
260 | 207 | "source": [ |
261 | | - "%%timeit\n", |
| 208 | + "%%timeit sleep()\n", |
| 209 | + "\n", |
262 | 210 | "compute_and_convert(h3.api.numpy_int)" |
263 | 211 | ] |
264 | 212 | }, |
|
269 | 217 | "source": [ |
270 | 218 | "# Speedup\n", |
271 | 219 | "\n", |
272 | | - "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`." |
| 220 | + "We typically see about a 6--8x speedup in the case of comparing:\n", |
| 221 | + "\n", |
| 222 | + "- computing with the `h3.api.basic_str` interface against\n", |
| 223 | + "- computing with the `h3.api.numpy_int`, and then converting the results back to `str`." |
273 | 224 | ] |
274 | 225 | }, |
275 | 226 | { |
276 | 227 | "cell_type": "code", |
277 | | - "execution_count": 16, |
| 228 | + "execution_count": 11, |
278 | 229 | "id": "188da0e8-efd8-43f5-9f45-b821e085b8a7", |
279 | 230 | "metadata": {}, |
280 | 231 | "outputs": [ |
281 | 232 | { |
282 | 233 | "data": { |
283 | 234 | "text/plain": [ |
284 | | - "6.922094508301405" |
| 235 | + "7.594278283485045" |
285 | 236 | ] |
286 | 237 | }, |
287 | | - "execution_count": 16, |
| 238 | + "execution_count": 11, |
288 | 239 | "metadata": {}, |
289 | 240 | "output_type": "execute_result" |
290 | 241 | } |
291 | 242 | ], |
292 | 243 | "source": [ |
293 | | - "# REMEMBER! update the numbers from the `%%timeit`s above!\n", |
| 244 | + "### REMEMBER! Update the numbers from the `%%timeit`s above!\n", |
294 | 245 | "\n", |
295 | | - "c_str = 54.2\n", |
296 | | - "cnc_numpy = 7.83\n", |
| 246 | + "c_str = 58.4\n", |
| 247 | + "cnc_numpy = 7.69\n", |
297 | 248 | "\n", |
298 | 249 | "c_str/cnc_numpy" |
299 | 250 | ] |
300 | 251 | }, |
301 | 252 | { |
302 | 253 | "cell_type": "code", |
303 | 254 | "execution_count": null, |
304 | | - "id": "233d4a44-f8e6-42d2-b54d-b6001c776102", |
| 255 | + "id": "326bea04-304f-4b03-81f2-2fc77420e429", |
305 | 256 | "metadata": {}, |
306 | 257 | "outputs": [], |
307 | 258 | "source": [] |
|
0 commit comments