1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* TreeMap benchmark class |
5
|
|
|
* |
6
|
|
|
* @author Christophe Demko <[email protected]> |
7
|
|
|
* @copyright Copyright (C) 2012-2018 Christophe Demko. All rights reserved. |
8
|
|
|
* |
9
|
|
|
* @license BSD 3-Clause License |
10
|
|
|
* |
11
|
|
|
* This file is part of the php-sorted-collections package https://github.com/chdemko/php-sorted-collections |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* TreeMap benchmark class |
16
|
|
|
* |
17
|
|
|
* @since 1.0.5 |
18
|
|
|
*/ |
19
|
|
|
class TreeMap |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var TreeMap The tree map |
23
|
|
|
* |
24
|
|
|
* @since 1.0.5 |
25
|
|
|
*/ |
26
|
|
|
protected $tree; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SortedMap The sorted map |
30
|
|
|
* |
31
|
|
|
* @since 1.0.5 |
32
|
|
|
*/ |
33
|
|
|
protected $data; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Provider for counts |
37
|
|
|
* |
38
|
|
|
* @return iterator Iterator on count |
39
|
|
|
* |
40
|
|
|
* @since 1.0.5 |
41
|
|
|
*/ |
42
|
|
|
public function provideCounts() |
43
|
|
|
{ |
44
|
|
|
yield array('count' => 100); |
45
|
|
|
yield array('count' => 1000); |
46
|
|
|
yield array('count' => 10000); |
47
|
|
|
yield array('count' => 100000); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Provider for counts |
52
|
|
|
* |
53
|
|
|
* @return iterator Iterator on type |
54
|
|
|
* |
55
|
|
|
* @since 1.0.5 |
56
|
|
|
*/ |
57
|
|
|
public function provideTypes() |
58
|
|
|
{ |
59
|
|
|
yield array('type' => 'tree'); |
60
|
|
|
yield array('type' => 'reversed'); |
61
|
|
|
yield array('type' => 'sub', 'from' => 0.30, 'to' => 0.70); |
62
|
|
|
yield array('type' => 'sub', 'from' => 0.40, 'to' => 0.80); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Create the tree map. |
67
|
|
|
* |
68
|
|
|
* @param array $params Array of parameters |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
* |
72
|
|
|
* @since 1.0.5 |
73
|
|
|
*/ |
74
|
|
|
public function init($params) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->tree = chdemko\SortedCollection\TreeMap::create(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create the sorted map. |
81
|
|
|
* |
82
|
|
|
* @param array $params Array of parameters |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
* |
86
|
|
|
* @since 1.0.5 |
87
|
|
|
*/ |
88
|
|
|
public function data($params) |
89
|
|
|
{ |
90
|
|
|
if (isset($params['type'])) |
91
|
|
|
{ |
92
|
|
|
switch ($params['type']) |
93
|
|
|
{ |
94
|
|
|
case 'tree': |
95
|
|
|
$this->data = $this->tree; |
|
|
|
|
96
|
|
|
break; |
97
|
|
|
case 'reversed': |
98
|
|
|
$this->data = chdemko\SortedCollection\ReversedMap::create($this->tree); |
|
|
|
|
99
|
|
|
break; |
100
|
|
|
case 'sub': |
101
|
|
|
if (isset($params['from']) && isset($params['to'])) |
102
|
|
|
{ |
103
|
|
|
$this->data = chdemko\SortedCollection\SubMap::create( |
|
|
|
|
104
|
|
|
$this->tree, |
|
|
|
|
105
|
|
|
(int) ($params['from'] * $params['count']), |
106
|
|
|
(int) ($params['to'] * $params['count']) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
else |
110
|
|
|
{ |
111
|
|
|
$this->data = chdemko\SortedCollection\SubMap::create( |
|
|
|
|
112
|
|
|
$this->tree, |
|
|
|
|
113
|
|
|
null, |
114
|
|
|
null |
115
|
|
|
); |
116
|
|
|
} |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
else |
121
|
|
|
{ |
122
|
|
|
$this->data = $this->tree; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Clear the tree map. |
128
|
|
|
* |
129
|
|
|
* @param array $params Array of parameters |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
* |
133
|
|
|
* @since 1.0.5 |
134
|
|
|
*/ |
135
|
|
|
public function finish($params) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$this->tree->clear(); |
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @BeforeMethods({"init", "data"}) |
142
|
|
|
* @AfterMethods({"finish"}) |
143
|
|
|
* @Revs(5) |
144
|
|
|
* @ParamProviders({"provideCounts"}) |
145
|
|
|
* |
146
|
|
|
* @param array $params Array of parameters |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
* |
150
|
|
|
* @since 1.0.5 |
151
|
|
|
*/ |
152
|
|
View Code Duplication |
public function benchFill($params) |
|
|
|
|
153
|
|
|
{ |
154
|
|
|
for ($i = 0; $i < $params['count']; $i++) |
155
|
|
|
{ |
156
|
|
|
$this->tree[$i] = $i; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @BeforeMethods({"init", "benchFill", "data"}) |
162
|
|
|
* @AfterMethods({"finish"}) |
163
|
|
|
* @Revs(5) |
164
|
|
|
* @ParamProviders({"provideCounts", "provideTypes"}) |
165
|
|
|
* |
166
|
|
|
* @param array $params Array of parameters |
167
|
|
|
* |
168
|
|
|
* @return void |
169
|
|
|
* |
170
|
|
|
* @since 1.0.5 |
171
|
|
|
*/ |
172
|
|
|
public function benchSearch($params) |
173
|
|
|
{ |
174
|
|
View Code Duplication |
if (isset($params['from'])) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
$min = (int) ($params['from'] * $params['count']); |
177
|
|
|
} |
178
|
|
|
else |
179
|
|
|
{ |
180
|
|
|
$min = 0; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
View Code Duplication |
if (isset($params['to'])) |
|
|
|
|
184
|
|
|
{ |
185
|
|
|
$max = (int) ($params['to'] * $params['count']); |
186
|
|
|
} |
187
|
|
|
else |
188
|
|
|
{ |
189
|
|
|
$max = $params['count']; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
for ($i = $min; $i < $max; $i++) |
193
|
|
|
{ |
194
|
|
|
$value = $this->data[$i]; |
|
|
|
|
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @BeforeMethods({"init", "benchFill", "data"}) |
200
|
|
|
* @AfterMethods({"finish"}) |
201
|
|
|
* @Revs(5) |
202
|
|
|
* @ParamProviders({"provideCounts"}) |
203
|
|
|
* |
204
|
|
|
* @param array $params Array of parameters |
205
|
|
|
* |
206
|
|
|
* @return void |
207
|
|
|
* |
208
|
|
|
* @since 1.0.5 |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
public function benchClean($params) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
for ($i = 0; $i < $params['count']; $i++) |
213
|
|
|
{ |
214
|
|
|
unset($this->tree[$i]); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.