|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bdf\Collection; |
|
4
|
|
|
|
|
5
|
|
|
use Bdf\Collection\Util\Hashable; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @Revs(20) |
|
9
|
|
|
* @Warmup(1) |
|
10
|
|
|
* @BeforeMethods({"initData"}) |
|
11
|
|
|
*/ |
|
12
|
|
|
class HashTableBench |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var SimpleHashableObject[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $data; |
|
18
|
|
|
|
|
19
|
|
|
private $table; |
|
20
|
|
|
|
|
21
|
|
|
private $collection; |
|
22
|
|
|
|
|
23
|
|
|
public function initData() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->data = []; |
|
26
|
|
|
$this->table = new HashTable(); |
|
27
|
|
|
$this->collection = new ArrayCollection(); |
|
28
|
|
|
|
|
29
|
|
|
srand(1000); |
|
30
|
|
|
|
|
31
|
|
|
for ($i = 0; $i < 10000; ++$i) { |
|
32
|
|
|
$this->data[] = $o = new SimpleHashableObject(rand(0, 1000), rand(0, 1000)); |
|
33
|
|
|
|
|
34
|
|
|
$this->table[$o->a] = $o; |
|
35
|
|
|
$this->collection[$o->a] = $o; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @Groups({"set"}) |
|
41
|
|
|
*/ |
|
42
|
|
|
public function bench_HashTable_set() |
|
43
|
|
|
{ |
|
44
|
|
|
$table = new HashTable(); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($this->data as $object) { |
|
47
|
|
|
$table[$object->a.':'.$object->b] = $object; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @Groups({"set"}) |
|
53
|
|
|
*/ |
|
54
|
|
|
public function bench_HashTable_set_array_key() |
|
55
|
|
|
{ |
|
56
|
|
|
$table = new HashTable(); |
|
57
|
|
|
|
|
58
|
|
|
foreach ($this->data as $object) { |
|
59
|
|
|
$table[[$object->a, $object->b]] = $object; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @Groups({"set"}) |
|
65
|
|
|
*/ |
|
66
|
|
|
public function bench_HashTable_set_object_key() |
|
67
|
|
|
{ |
|
68
|
|
|
$table = new HashTable(); |
|
69
|
|
|
|
|
70
|
|
|
foreach ($this->data as $object) { |
|
71
|
|
|
$table[$object] = $object; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @Groups({"set"}) |
|
77
|
|
|
*/ |
|
78
|
|
|
public function bench_ArrayCollection_set() |
|
79
|
|
|
{ |
|
80
|
|
|
$table = new ArrayCollection(); |
|
81
|
|
|
|
|
82
|
|
|
foreach ($this->data as $object) { |
|
83
|
|
|
$table[$object->a.':'.$object->b] = $object; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @Groups({"get"}) |
|
89
|
|
|
*/ |
|
90
|
|
|
public function bench_HashTable_has_get() |
|
91
|
|
|
{ |
|
92
|
|
|
for ($i = 0; $i < 1000; ++$i) { |
|
93
|
|
|
if (isset($this->table[$i])) { |
|
94
|
|
|
$this->table[$i]; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @Groups({"get"}) |
|
101
|
|
|
*/ |
|
102
|
|
|
public function bench_ArrayCollection_has_get() |
|
103
|
|
|
{ |
|
104
|
|
|
for ($i = 0; $i < 1000; ++$i) { |
|
105
|
|
|
if (isset($this->collection[$i])) { |
|
106
|
|
|
$this->collection[$i]; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
class SimpleHashableObject implements Hashable |
|
113
|
|
|
{ |
|
114
|
|
|
public $a; |
|
115
|
|
|
public $b; |
|
116
|
|
|
|
|
117
|
|
|
public function __construct($a, $b) |
|
118
|
|
|
{ |
|
119
|
|
|
$this->a = $a; |
|
120
|
|
|
$this->b = $b; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function a() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->a; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function b() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->b; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function hash() |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->a.':'.$this->b; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|