質問編集履歴

2

様々な頂点数で時間を測った結果を追記

2019/07/22 01:53

投稿

gottadiveintopy
gottadiveintopy

スコア736

test CHANGED
File without changes
test CHANGED
@@ -141,3 +141,223 @@
141
141
 
142
142
 
143
143
  になると思います。
144
+
145
+
146
+
147
+ ### 追記
148
+
149
+
150
+
151
+ 私が書いた上二つの方法と`hayataka2049`さんの物と`bamboo-nova`さんの物を、頂点数を色々変えて実行時間を測ってみたので載せておきます。
152
+
153
+
154
+
155
+ ```python3
156
+
157
+ def method_1(points):
158
+
159
+ try:
160
+
161
+ it = iter(points)
162
+
163
+ max_x = min_x = next(it)
164
+
165
+ max_y = min_y = next(it)
166
+
167
+ while True:
168
+
169
+ x = next(it)
170
+
171
+ max_x = max(max_x, x)
172
+
173
+ min_x = min(min_x, x)
174
+
175
+ y = next(it)
176
+
177
+ max_y = max(max_y, y)
178
+
179
+ min_y = min(min_y, y)
180
+
181
+ except StopIteration:
182
+
183
+ pass
184
+
185
+ return (min_x, min_y, max_x, max_y)
186
+
187
+
188
+
189
+
190
+
191
+ def method_2(points):
192
+
193
+ half_length = len(points) // 2
194
+
195
+ x_list = [None] * half_length
196
+
197
+ y_list = [None] * half_length
198
+
199
+ x_index = 0
200
+
201
+ y_index = 0
202
+
203
+ try:
204
+
205
+ it = iter(points)
206
+
207
+ while True:
208
+
209
+ x_list[x_index] = next(it)
210
+
211
+ y_list[y_index] = next(it)
212
+
213
+ x_index += 1
214
+
215
+ y_index += 1
216
+
217
+ except StopIteration:
218
+
219
+ pass
220
+
221
+ return (min(x_list), min(y_list), max(x_list), max(y_list))
222
+
223
+
224
+
225
+
226
+
227
+ def method_hayataka(points):
228
+
229
+ even = points[::2]
230
+
231
+ odd = points[1::2]
232
+
233
+ return min(even), min(odd), max(even), max(odd)
234
+
235
+
236
+
237
+
238
+
239
+ def method_bamboo(points):
240
+
241
+ x_list = []
242
+
243
+ y_list = []
244
+
245
+ it = iter(points)
246
+
247
+ for v in it:
248
+
249
+ x_list.append(v)
250
+
251
+ y_list.append(next(it))
252
+
253
+ return (min(x_list), min(y_list), max(x_list), max(y_list))
254
+
255
+
256
+
257
+
258
+
259
+ import random
260
+
261
+ import timeit
262
+
263
+
264
+
265
+ for num_points in (10, 50, 100, 150, 200, 5000):
266
+
267
+ print(f'\n頂点数: {num_points // 2}')
268
+
269
+ points = [random.randrange(1000)%100 for _ in range(num_points)]
270
+
271
+ print('method_1():', timeit.timeit(lambda :method_1(points), number=1000))
272
+
273
+ print('method_2():', timeit.timeit(lambda :method_2(points), number=1000))
274
+
275
+ print('method_hayataka():', timeit.timeit(lambda :method_hayataka(points), number=1000))
276
+
277
+ print('method_bamboo():', timeit.timeit(lambda :method_bamboo(points), number=1000))
278
+
279
+ ```
280
+
281
+
282
+
283
+ ```text
284
+
285
+ 頂点数: 5
286
+
287
+ method_1(): 0.027533224085345864
288
+
289
+ method_2(): 0.025170729029923677
290
+
291
+ method_hayataka(): 0.013926777057349682
292
+
293
+ method_bamboo(): 0.01895964704453945
294
+
295
+
296
+
297
+ 頂点数: 25
298
+
299
+ method_1(): 0.08861580200027674
300
+
301
+ method_2(): 0.0367969439830631
302
+
303
+ method_hayataka(): 0.00978929502889514
304
+
305
+ method_bamboo(): 0.03307596605736762
306
+
307
+
308
+
309
+ 頂点数: 50
310
+
311
+ method_1(): 0.11920186190400273
312
+
313
+ method_2(): 0.05401027004700154
314
+
315
+ method_hayataka(): 0.015701663098298013
316
+
317
+ method_bamboo(): 0.0533315270440653
318
+
319
+
320
+
321
+ 頂点数: 75
322
+
323
+ method_1(): 0.1530481429072097
324
+
325
+ method_2(): 0.07044070307165384
326
+
327
+ method_hayataka(): 0.018882130039855838
328
+
329
+ method_bamboo(): 0.058508455054834485
330
+
331
+
332
+
333
+ 頂点数: 100
334
+
335
+ method_1(): 0.22204544604755938
336
+
337
+ method_2(): 0.08647163794375956
338
+
339
+ method_hayataka(): 0.02639697107952088
340
+
341
+ method_bamboo(): 0.06969467597082257
342
+
343
+
344
+
345
+ 頂点数: 2500
346
+
347
+ method_1(): 4.2770471959374845
348
+
349
+ method_2(): 1.7805748030077666
350
+
351
+ method_hayataka(): 0.2754193090368062
352
+
353
+ method_bamboo(): 1.3511152860010043
354
+
355
+ ```
356
+
357
+
358
+
359
+ というわけで結果はすべての場合において
360
+
361
+
362
+
363
+ `method_hayataka()` > `method_bamboo()` > `method_2()` > `method_1()`の順に速いとなりました。

1

コードのbugを修正

2019/07/22 01:53

投稿

gottadiveintopy
gottadiveintopy

スコア736

test CHANGED
File without changes
test CHANGED
@@ -93,3 +93,51 @@
93
93
 
94
94
 
95
95
  - Python: 3.7.1
96
+
97
+
98
+
99
+ ### 追記
100
+
101
+
102
+
103
+ `method_1()`がそもそも正しく動いていませんでした。正しくは
104
+
105
+
106
+
107
+ ```python3
108
+
109
+ def method_1(points):
110
+
111
+ try:
112
+
113
+ it = iter(points)
114
+
115
+ max_x = min_x = next(it)
116
+
117
+ max_y = min_y = next(it)
118
+
119
+ while True:
120
+
121
+ x = next(it)
122
+
123
+ max_x = max(max_x, x)
124
+
125
+ min_x = min(min_x, x)
126
+
127
+ y = next(it)
128
+
129
+ max_y = max(max_y, y)
130
+
131
+ min_y = min(min_y, y)
132
+
133
+ except StopIteration:
134
+
135
+ pass
136
+
137
+ return (min_x, min_y, max_x, max_y)
138
+
139
+ ```
140
+
141
+
142
+
143
+ になると思います。