-
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathArrayKeysBench.php
More file actions
67 lines (59 loc) · 1.44 KB
/
Copy pathArrayKeysBench.php
File metadata and controls
67 lines (59 loc) · 1.44 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/*
* This file is part of the PHPBench package
*
* (c) Daniel Leech <daniel@dantleech.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PhpBench\Tests\Functional\benchmarks;
/**
* This example benchmarks array_key_exists vs. isset vs. in_array.
*
* @BeforeMethods({"init"})
* @Revs(1000)
* @Iterations(4)
* @Groups({"array_keys"})
* @ParamProviders({"provideNbElements"})
*/
class ArrayKeysBench
{
private $array;
private $values;
private $index = 0;
public function init($params)
{
$this->array = array_fill(0, $params['nb_elements'], 'this is a test');
$this->values = array_combine(array_keys($this->array), array_keys($this->array));
}
public function benchArrayKeyExists()
{
array_key_exists($this->index++, $this->array);
}
public function benchIsset()
{
isset($this->array[$this->index++]);
}
public function benchInArray()
{
in_array($this->index++, $this->values);
}
public function provideNbElements()
{
return array(
array(
'nb_elements' => 10,
),
array(
'nb_elements' => 100,
),
array(
'nb_elements' => 1000,
),
array(
'nb_elements' => 10000,
),
);
}
}