回答編集履歴

2

追記

2019/09/30 00:14

投稿

退会済みユーザー
test CHANGED
@@ -179,3 +179,203 @@
179
179
  paiza.io を使用したので計測値は結構揺れました。(timeout するんで、回数も絞ってますw)
180
180
 
181
181
  多分、php のバージョンによっても異なるはずなので、自身の環境で確認してみてください。
182
+
183
+
184
+
185
+ **さらに追記**
186
+
187
+ ```php
188
+
189
+ <?php
190
+
191
+ function test_in_array($arr, $list){
192
+
193
+ $res = [];
194
+
195
+ foreach($arr as $value){
196
+
197
+ if(in_array($value[0], $list)){
198
+
199
+ $res[] = $value;
200
+
201
+ }
202
+
203
+ }
204
+
205
+ return $res;
206
+
207
+ }
208
+
209
+
210
+
211
+ function test_array_key_exists($arr, $list){
212
+
213
+ $list2 = array_fill_keys($list, TRUE);
214
+
215
+ $res = [];
216
+
217
+ foreach($arr as $value){
218
+
219
+ if(array_key_exists($value[0], $list2)){
220
+
221
+ $res[] = $value;
222
+
223
+ }
224
+
225
+ }
226
+
227
+ return $res;
228
+
229
+ }
230
+
231
+
232
+
233
+ function test_foreach($arr, $list){
234
+
235
+ $res = [];
236
+
237
+ foreach ($arr as $value){
238
+
239
+ foreach($list as $num){
240
+
241
+ if($value[0]===$num){
242
+
243
+ $res[] = $value;
244
+
245
+ }
246
+
247
+ }
248
+
249
+ }
250
+
251
+ return $res;
252
+
253
+ }
254
+
255
+
256
+
257
+ function test_sort($arr, $list){
258
+
259
+ $res = [];
260
+
261
+ foreach($arr as $value){
262
+
263
+ if(!isset($tmp[$value[0]])){
264
+
265
+ $tmp[$value[0]] = [];
266
+
267
+ }
268
+
269
+ $tmp[$value[0]][] = $value;
270
+
271
+ }
272
+
273
+ foreach($list as $num){
274
+
275
+ if(isset($tmp[$num])){
276
+
277
+ $res[] = $tmp[$num];
278
+
279
+ }
280
+
281
+ }
282
+
283
+ return $res;
284
+
285
+ }
286
+
287
+
288
+
289
+ foreach([[10000, 10], [1000, 10], [10000, 100], [100000, 100],] as [$arr_max, $list_max]){
290
+
291
+ $arr = [];
292
+
293
+ for($i = 0; $i < $arr_max; $i++){
294
+
295
+ $arr[] = [
296
+
297
+ rand(0, 100),
298
+
299
+ 'hoge',
300
+
301
+ 'fuga',
302
+
303
+ ];
304
+
305
+ }
306
+
307
+
308
+
309
+ $list = [];
310
+
311
+ for($i = 0; $i < $list_max; $i++){
312
+
313
+ $list[] = rand(0, 100);
314
+
315
+ }
316
+
317
+
318
+
319
+ echo 'test : arr_max = ' . $arr_max . ', list_max = ' . $list_max . PHP_EOL;
320
+
321
+ foreach(['test_in_array', 'test_foreach', 'test_sort', 'test_array_key_exists'] as $func){
322
+
323
+ $time_start = microtime(true);
324
+
325
+ $func($arr, $list);
326
+
327
+ $time = microtime(true) - $time_start;
328
+
329
+ printf("$func \t %.7f 秒\n",$time);
330
+
331
+ }
332
+
333
+ }
334
+
335
+ ```
336
+
337
+ ```
338
+
339
+ test : arr_max = 10000, list_max = 10
340
+
341
+ test_in_array 0.0010860 秒
342
+
343
+ test_foreach 0.0018811 秒
344
+
345
+ test_sort 0.0021858 秒
346
+
347
+ test_array_key_exists 0.0003760 秒
348
+
349
+ test : arr_max = 1000, list_max = 10
350
+
351
+ test_in_array 0.0000389 秒
352
+
353
+ test_foreach 0.0001709 秒
354
+
355
+ test_sort 0.0000539 秒
356
+
357
+ test_array_key_exists 0.0000341 秒
358
+
359
+ test : arr_max = 10000, list_max = 100
360
+
361
+ test_in_array 0.0010271 秒
362
+
363
+ test_foreach 0.0149219 秒
364
+
365
+ test_sort 0.0006859 秒
366
+
367
+ test_array_key_exists 0.0004339 秒
368
+
369
+ test : arr_max = 100000, list_max = 100
370
+
371
+ test_in_array 0.0258038 秒
372
+
373
+ test_foreach 0.1712370 秒
374
+
375
+ test_sort 0.0291610 秒
376
+
377
+ test_array_key_exists 0.0179920 秒
378
+
379
+ ```
380
+
381
+ 結論:array_key_exists() は早い!w

1

追記

2019/09/30 00:14

投稿

退会済みユーザー
test CHANGED
@@ -2,4 +2,180 @@
2
2
 
3
3
 
4
4
 
5
- もし`$ar`が DB から取り出した値なのであれば、取り出すときの構造を工夫すれば効率の良い取り出しができるかと。
5
+ もし`$arr`が DB から取り出した値なのであれば、取り出すときの構造を工夫すれば効率の良い取り出しができるかと。
6
+
7
+
8
+
9
+ **追記**
10
+
11
+ 興味本位で、配列の持ち方の変更まで含めてパフォーマンスがどうなるか、コードを書いてみました。
12
+
13
+ ```php
14
+
15
+ <?php
16
+
17
+ function test_in_array($arr, $list){
18
+
19
+ $res = [];
20
+
21
+ foreach($arr as $value){
22
+
23
+ if(in_array($value[0], $list)){
24
+
25
+ $res[] = $value;
26
+
27
+ }
28
+
29
+ }
30
+
31
+ return $res;
32
+
33
+ }
34
+
35
+
36
+
37
+ function test_foreach($arr, $list){
38
+
39
+ $res = [];
40
+
41
+ foreach ($arr as $value){
42
+
43
+ foreach($list as $num){
44
+
45
+ if($value[0]===$num){
46
+
47
+ $res[] = $value;
48
+
49
+ }
50
+
51
+ }
52
+
53
+ }
54
+
55
+ return $res;
56
+
57
+ }
58
+
59
+
60
+
61
+ function test_sort($arr, $list){
62
+
63
+ $res = [];
64
+
65
+ foreach($arr as $value){
66
+
67
+ if(!isset($tmp[$value[0]])){
68
+
69
+ $tmp[$value[0]] = [];
70
+
71
+ }
72
+
73
+ $tmp[$value[0]][] = $value;
74
+
75
+ }
76
+
77
+ foreach($list as $num){
78
+
79
+ if(isset($tmp[$num])){
80
+
81
+ $res[] = $tmp[$num];
82
+
83
+ }
84
+
85
+ }
86
+
87
+ return $res;
88
+
89
+ }
90
+
91
+
92
+
93
+ foreach([[10000, 10], [1000, 10], [10000, 100], [100000, 100],] as [$arr_max, $list_max]){
94
+
95
+ $arr = [];
96
+
97
+ for($i = 0; $i < $arr_max; $i++){
98
+
99
+ $arr[] = [
100
+
101
+ rand(0, 100),
102
+
103
+ 'hoge',
104
+
105
+ 'fuga',
106
+
107
+ ];
108
+
109
+ }
110
+
111
+
112
+
113
+ $list = [];
114
+
115
+ for($i = 0; $i < $list_max; $i++){
116
+
117
+ $list[] = rand(0, 100);
118
+
119
+ }
120
+
121
+ echo 'test : arr_max = ' . $arr_max . ', list_max = ' . $list_max . PHP_EOL;
122
+
123
+ foreach(['test_in_array', 'test_foreach', 'test_sort'] as $func){
124
+
125
+ $time_start = microtime(true);
126
+
127
+ $func($arr, $list);
128
+
129
+ $time = microtime(true) - $time_start;
130
+
131
+ printf("$func \t %.7f 秒\n",$time);
132
+
133
+ }
134
+
135
+ }
136
+
137
+ ```
138
+
139
+ in_array が圧倒的に早いかと思ってましたが、そうでもないですね。
140
+
141
+ ```php
142
+
143
+ test : arr_max = 10000, list_max = 10
144
+
145
+ test_in_array 0.0011530 秒
146
+
147
+ test_foreach 0.0019360 秒
148
+
149
+ test_sort 0.0010860 秒
150
+
151
+ test : arr_max = 1000, list_max = 10
152
+
153
+ test_in_array 0.0000391 秒
154
+
155
+ test_foreach 0.0001721 秒
156
+
157
+ test_sort 0.0000541 秒
158
+
159
+ test : arr_max = 10000, list_max = 100
160
+
161
+ test_in_array 0.0010500 秒
162
+
163
+ test_foreach 0.0152941 秒
164
+
165
+ test_sort 0.0007241 秒
166
+
167
+ test : arr_max = 100000, list_max = 100
168
+
169
+ test_in_array 0.0273101 秒
170
+
171
+ test_foreach 0.1742241 秒
172
+
173
+ test_sort 0.0325580 秒
174
+
175
+ ```
176
+
177
+ やはり実測してみると面白いです。
178
+
179
+ paiza.io を使用したので計測値は結構揺れました。(timeout するんで、回数も絞ってますw)
180
+
181
+ 多分、php のバージョンによっても異なるはずなので、自身の環境で確認してみてください。