使用各种循环结构把一千万长度的数组的每个值自增1

测试用例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
class Test
{
private $data;

public function __construct($data)
{
$this->data = $data;
}

public function second()
{
list($t1 , $t2) = explode(' ' , microtime());
return (float)sprintf('%.0f' , (floatval($t1) + floatval($t2)) * 1000);
}

public function foreach()
{
foreach ($this->data as $index => &$item) {
$item++;
}
}

public function array_reduce()
{
array_reduce($this->data , function (&$it) {
$it++;
});
}

public function array_map()
{
array_map(function (&$it) {
$it++;
} , $this->data);
}

public function array_walk()
{
array_walk($this->data , function (&$it , $key) {
$it++;
});
}

public function array_filter_use_both()
{
array_filter($this->data , function (&$it , $key) {
$it++;
} , ARRAY_FILTER_USE_BOTH);
}

public function array_filter_use_key()
{
array_filter($this->data , function ($key) {
$this->data[$key]++;
} , ARRAY_FILTER_USE_KEY);
}
}

$data = [
'array_map' ,
'array_walk' ,
'array_filter_use_key' ,
'array_filter_use_both' ,
'array_reduce' ,
'foreach' ,
];

$test = new Test(range(0 , 10000000));
array_reduce($data , function ($res , $it) use ($test) {
$start = $test->second();
$test->{$it}();
echo $it . ' ' . ((float)$test->second() - (float)$start) / 1000 . 's' . PHP_EOL;
});

三次执行获得了如下测试结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
> php index.php
array_map 8.152s
array_walk 2.257s
array_filter_use_key 3.76s
array_filter_use_both 0.449s
array_reduce 0.428s
foreach 0.216s

> php index.php
array_map 8.127s
array_walk 2.447s
array_filter_use_key 1.71s
array_filter_use_both 0.46s
array_reduce 0.447s
foreach 0.217s

> php index.php
array_map 10.001s
array_walk 5.566s
array_filter_use_key 1.958s
array_filter_use_both 0.464s
array_reduce 0.44s
foreach 0.222s

综合排名

  1. foreach
  2. array_reduce
  3. array_filter_use_both
  4. array_filter_use_key
  5. array_walk
  6. array_map

所以还是老老实实相信 foreach