1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Collection; |
4
|
|
|
|
5
|
|
|
use Bdf\Collection\Util\Hashable; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @Revs(10) |
9
|
|
|
* @BeforeMethods({"initData"}) |
10
|
|
|
*/ |
11
|
|
|
class HashSetBench |
12
|
|
|
{ |
13
|
|
|
private $data = []; |
14
|
|
|
|
15
|
|
|
public function initData() |
16
|
|
|
{ |
17
|
|
|
$this->data = [ |
18
|
|
|
'100' => self::intArray(100), |
19
|
|
|
'1000' => self::intArray(1000), |
20
|
|
|
'10000' => self::intArray(10000), |
21
|
|
|
'100000' => self::intArray(100000), |
22
|
|
|
'int' => self::intArray(10000), |
23
|
|
|
'simpleObject' => self::objectArray(10000, SimpleObject::class), |
24
|
|
|
'customHashObject' => self::objectArray(10000, CustomHashObject::class), |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
$set = new HashSet(); |
28
|
|
|
|
29
|
|
|
foreach ($this->data['customHashObject'] as $i) { |
30
|
|
|
$set->add($i); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$this->data['HashSet'] = $set; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @ParamProviders("provideArrayData") |
38
|
|
|
* @Groups({"distinct"}) |
39
|
|
|
*/ |
40
|
|
|
public function bench_HashSet_add_toArray($param) |
41
|
|
|
{ |
42
|
|
|
$set = new HashSet(); |
43
|
|
|
|
44
|
|
|
foreach ($this->data[$param['data']] as $value) { |
45
|
|
|
$set->add($value); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $set->toArray(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @ParamProviders("provideArrayData") |
53
|
|
|
* @Groups({"distinct"}) |
54
|
|
|
*/ |
55
|
|
|
public function bench_array_unique($param) |
56
|
|
|
{ |
57
|
|
|
return array_unique($this->data[$param['data']], SORT_REGULAR); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @ParamProviders("provideSizes") |
62
|
|
|
* @Groups({"complexity", "HashSet"}) |
63
|
|
|
*/ |
64
|
|
|
public function bench_HashSet_add_complexity($param) |
65
|
|
|
{ |
66
|
|
|
$set = new HashSet(); |
67
|
|
|
|
68
|
|
|
foreach ($this->data[$param['data']] as $value) { |
69
|
|
|
$set->add($value); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $set->toArray(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @ParamProviders("provideSizes") |
77
|
|
|
* @Groups({"complexity", "array_unique"}) |
78
|
|
|
*/ |
79
|
|
|
public function bench_array_unique_complexity($param) |
80
|
|
|
{ |
81
|
|
|
return array_unique($this->data[$param['data']], SORT_REGULAR); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @Groups({"contains"}) |
86
|
|
|
*/ |
87
|
|
|
public function bench_HashSet_contains() |
88
|
|
|
{ |
89
|
|
|
$this->data['HashSet']->contains(new CustomHashObject(2, 3)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Groups({"contains"}) |
94
|
|
|
*/ |
95
|
|
|
public function bench_array_search() |
96
|
|
|
{ |
97
|
|
|
array_search(new CustomHashObject(2, 3), $this->data['customHashObject']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function provideArrayData() |
101
|
|
|
{ |
102
|
|
|
return [ |
103
|
|
|
['data' => 'int'], |
104
|
|
|
['data' => 'simpleObject'], |
105
|
|
|
['data' => 'customHashObject'], |
106
|
|
|
]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function provideSizes() |
110
|
|
|
{ |
111
|
|
|
return [ |
112
|
|
|
['data' => 100], |
113
|
|
|
['data' => 1000], |
114
|
|
|
['data' => 10000], |
115
|
|
|
['data' => 100000], |
116
|
|
|
]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private static function intArray($size) |
120
|
|
|
{ |
121
|
|
|
$array = []; |
122
|
|
|
|
123
|
|
|
while ($size--) { |
124
|
|
|
$array[] = rand(0, 100); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $array; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private static function objectArray($size, $className) |
131
|
|
|
{ |
132
|
|
|
$array = []; |
133
|
|
|
|
134
|
|
|
while ($size--) { |
135
|
|
|
$array[] = new $className(rand(0, 10), rand(0, 10)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $array; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
class SimpleObject |
143
|
|
|
{ |
144
|
|
|
private $a; |
145
|
|
|
private $b; |
146
|
|
|
|
147
|
|
|
public function __construct($a, $b) |
148
|
|
|
{ |
149
|
|
|
$this->a = $a; |
150
|
|
|
$this->b = $b; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
class CustomHashObject implements Hashable |
155
|
|
|
{ |
156
|
|
|
private $a; |
157
|
|
|
private $b; |
158
|
|
|
|
159
|
|
|
public function __construct($a, $b) |
160
|
|
|
{ |
161
|
|
|
$this->a = $a; |
162
|
|
|
$this->b = $b; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function hash() |
166
|
|
|
{ |
167
|
|
|
return $this->a.':'.$this->b; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|