Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 490e07d

Browse files
committed
clean up timing code
1 parent 90c688c commit 490e07d

File tree

1 file changed

+43
-92
lines changed

1 file changed

+43
-92
lines changed

‎notebooks/time_h3_apis.ipynb‎

Lines changed: 43 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"# API Speed Comparison\n",
99
"\n",
1010
"We time a simple toy computational task across the various APIs.\n",
11+
"\n",
1112
"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",
1213
"and then converting the results to the more familiar format of Python `str` objects."
1314
]
@@ -21,9 +22,7 @@
2122
"source": [
2223
"import h3\n",
2324
"import h3.api.numpy_int\n",
24-
"from time import sleep\n",
2525
"\n",
26-
"sleep_seconds = 20\n",
2726
"\n",
2827
"def compute(h3_lib, N=100):\n",
2928
" h = h3_lib.geo_to_h3(0, 0, 9)\n",
@@ -48,17 +47,19 @@
4847
"source": [
4948
"# Compute with each API\n",
5049
"\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)."
5251
]
5352
},
5453
{
5554
"cell_type": "code",
5655
"execution_count": 2,
57-
"id": "1889e459-0482-44f8-a4a9-a363f6a64231",
56+
"id": "e636e193-22ff-440f-a5b8-4c073c8f8a57",
5857
"metadata": {},
5958
"outputs": [],
6059
"source": [
61-
"sleep(sleep_seconds)"
60+
"def sleep():\n",
61+
" import time\n",
62+
" time.sleep(2)"
6263
]
6364
},
6465
{
@@ -71,99 +72,73 @@
7172
"name": "stdout",
7273
"output_type": "stream",
7374
"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"
7576
]
7677
}
7778
],
7879
"source": [
79-
"%%timeit\n",
80+
"%%timeit sleep()\n",
81+
"\n",
8082
"compute(h3.api.basic_str)"
8183
]
8284
},
8385
{
8486
"cell_type": "code",
8587
"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,
9688
"id": "6d80bca9-c0f5-4aaa-8488-f7bd174c7ee3",
9789
"metadata": {},
9890
"outputs": [
9991
{
10092
"name": "stdout",
10193
"output_type": "stream",
10294
"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"
10496
]
10597
}
10698
],
10799
"source": [
108-
"%%timeit\n",
100+
"%%timeit sleep()\n",
101+
"\n",
109102
"compute(h3.api.basic_int)"
110103
]
111104
},
112105
{
113106
"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,
125108
"id": "f2713e84-c4fb-4a60-b913-c1fb7ea7fd4b",
126109
"metadata": {},
127110
"outputs": [
128111
{
129112
"name": "stdout",
130113
"output_type": "stream",
131114
"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"
133116
]
134117
}
135118
],
136119
"source": [
137-
"%%timeit\n",
120+
"%%timeit sleep()\n",
121+
"\n",
138122
"compute(h3.api.numpy_int)"
139123
]
140124
},
141125
{
142126
"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,
154128
"id": "e9931ac6-eeba-4168-92e1-f8f330b1ca42",
155129
"metadata": {},
156130
"outputs": [
157131
{
158132
"name": "stdout",
159133
"output_type": "stream",
160134
"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"
162136
]
163137
}
164138
],
165139
"source": [
166-
"%%timeit\n",
140+
"%%timeit sleep()\n",
141+
"\n",
167142
"compute(h3.api.memview_int)"
168143
]
169144
},
@@ -177,88 +152,61 @@
177152
},
178153
{
179154
"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,
191156
"id": "51c02c9f-0a03-4e84-943d-09969949b83d",
192157
"metadata": {},
193158
"outputs": [
194159
{
195160
"name": "stdout",
196161
"output_type": "stream",
197162
"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"
199164
]
200165
}
201166
],
202167
"source": [
203-
"%%timeit\n",
168+
"%%timeit sleep()\n",
169+
"\n",
204170
"compute_and_convert(h3.api.basic_int)"
205171
]
206172
},
207173
{
208174
"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,
220176
"id": "b11e3d6d-58d3-418f-9798-2a80dc16b133",
221177
"metadata": {},
222178
"outputs": [
223179
{
224180
"name": "stdout",
225181
"output_type": "stream",
226182
"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"
228184
]
229185
}
230186
],
231187
"source": [
232-
"%%timeit\n",
188+
"%%timeit sleep()\n",
189+
"\n",
233190
"compute_and_convert(h3.api.memview_int)"
234191
]
235192
},
236193
{
237194
"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,
249196
"id": "5cf6e8a8-583d-4309-8ed2-5b4c31c8765a",
250197
"metadata": {},
251198
"outputs": [
252199
{
253200
"name": "stdout",
254201
"output_type": "stream",
255202
"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"
257204
]
258205
}
259206
],
260207
"source": [
261-
"%%timeit\n",
208+
"%%timeit sleep()\n",
209+
"\n",
262210
"compute_and_convert(h3.api.numpy_int)"
263211
]
264212
},
@@ -269,39 +217,42 @@
269217
"source": [
270218
"# Speedup\n",
271219
"\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`."
273224
]
274225
},
275226
{
276227
"cell_type": "code",
277-
"execution_count": 16,
228+
"execution_count": 11,
278229
"id": "188da0e8-efd8-43f5-9f45-b821e085b8a7",
279230
"metadata": {},
280231
"outputs": [
281232
{
282233
"data": {
283234
"text/plain": [
284-
"6.922094508301405"
235+
"7.594278283485045"
285236
]
286237
},
287-
"execution_count": 16,
238+
"execution_count": 11,
288239
"metadata": {},
289240
"output_type": "execute_result"
290241
}
291242
],
292243
"source": [
293-
"# REMEMBER! update the numbers from the `%%timeit`s above!\n",
244+
"### REMEMBER! Update the numbers from the `%%timeit`s above!\n",
294245
"\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",
297248
"\n",
298249
"c_str/cnc_numpy"
299250
]
300251
},
301252
{
302253
"cell_type": "code",
303254
"execution_count": null,
304-
"id": "233d4a44-f8e6-42d2-b54d-b6001c776102",
255+
"id": "326bea04-304f-4b03-81f2-2fc77420e429",
305256
"metadata": {},
306257
"outputs": [],
307258
"source": []

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /