-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArrayWalkTest.php
More file actions
49 lines (39 loc) · 1.19 KB
/
Copy pathArrayWalkTest.php
File metadata and controls
49 lines (39 loc) · 1.19 KB
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
<?php
/**
* Created by PhpStorm.
* User: lianjia
* Date: 2018/9/17
* Time: 上午11:22
*/
use PHPUnit\Framework\TestCase;
class ArrayWalkTest extends TestCase
{
private $retryTimes = 10;
private $test_arr = []; // keys and values
public function testForeach() {
$timer = Benchmark::getInstance();
$this -> test_arr = array_combine(range(1, 100000), range(1, 100000));
$cost = $timer -> tryManyTimes(function () {
$i = 0;
foreach ($this -> test_arr as $item) {
if ($item == null) {
++$i;
}
}
}, $this -> retryTimes);
echo "Foreach Costs :: " . $cost . PHP_EOL;
}
public function testArrayWalk() {
$timer = Benchmark::getInstance();
$this -> test_arr = array_combine(range(1, 100000), range(1, 100000));
$cost = $timer -> tryManyTimes(function () {
$i = 0;
array_walk($this -> test_arr, function (&$value) use (&$i) {
if ($value == null) {
++$i;
}
});
}, $this -> retryTimes);
echo "ArrayWalk Costs :: " . $cost . PHP_EOL;
}
}