1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bdf\Collection; |
4
|
|
|
|
5
|
|
|
use Bdf\Collection\Stream\ArrayStream; |
6
|
|
|
use Bdf\Collection\Stream\MutableArrayStream; |
7
|
|
|
use Bdf\Collection\Stream\Accumulator\Accumulators; |
8
|
|
|
use Bdf\Collection\Util\Functor\Transformer\Getter; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @Revs(10) |
12
|
|
|
* @Warmup(1) |
13
|
|
|
* @BeforeMethods({"initData"}) |
14
|
|
|
*/ |
15
|
|
|
class StreamBench |
16
|
|
|
{ |
17
|
|
|
private $data; |
18
|
|
|
|
19
|
|
|
public function initData() |
20
|
|
|
{ |
21
|
|
|
$this->data = []; |
22
|
|
|
|
23
|
|
|
srand(1000); |
24
|
|
|
|
25
|
|
|
for ($i = 0; $i < 1000; ++$i) { |
26
|
|
|
$this->data[] = new MyEntity($i, new MyEntity(rand(0, 50))); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @Groups({"transform"}) |
32
|
|
|
*/ |
33
|
|
|
public function bench_Stream_complex_transform() |
34
|
|
|
{ |
35
|
|
|
$stream = new ArrayStream($this->data); |
36
|
|
|
|
37
|
|
|
return $stream |
38
|
|
|
->filter(function ($e) { return $e->id % 3 === 0; }) |
39
|
|
|
->map(function ($e) { return $e->sub; }) |
40
|
|
|
->distinct() |
41
|
|
|
->toArray() |
42
|
|
|
; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @Groups({"transform"}) |
47
|
|
|
*/ |
48
|
|
|
public function bench_MutableArrayStream_complex_transform() |
49
|
|
|
{ |
50
|
|
|
$stream = new MutableArrayStream($this->data); |
51
|
|
|
|
52
|
|
|
return $stream |
53
|
|
|
->filter(function ($e) { return $e->id % 3 === 0; }) |
54
|
|
|
->map(function ($e) { return $e->sub; }) |
55
|
|
|
->distinct() |
56
|
|
|
->toArray() |
57
|
|
|
; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @Groups({"transform"}) |
62
|
|
|
*/ |
63
|
|
|
public function bench_native_array_complex_transform() |
64
|
|
|
{ |
65
|
|
|
$array = $this->data; |
66
|
|
|
|
67
|
|
|
return array_unique( |
68
|
|
|
array_map( |
69
|
|
|
function ($e) { return $e->sub; }, |
70
|
|
|
array_filter($array, function ($e) { return $e->id % 3 === 0; }) |
71
|
|
|
), |
72
|
|
|
SORT_REGULAR |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @Groups({"reduce"}) |
78
|
|
|
*/ |
79
|
|
|
public function bench_reduce_with_functor() |
80
|
|
|
{ |
81
|
|
|
return (new ArrayStream(range(0, 100)))->reduce(Accumulators::sum()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @Groups({"reduce"}) |
86
|
|
|
*/ |
87
|
|
|
public function bench_reduce_with_closure() |
88
|
|
|
{ |
89
|
|
|
return (new ArrayStream(range(0, 100)))->reduce(function ($a, $b) { return $a + $b; }); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @Groups({"map"}) |
94
|
|
|
*/ |
95
|
|
|
public function bench_map_getter_functor() |
96
|
|
|
{ |
97
|
|
|
return (new ArrayStream($this->data))->map(new Getter('sub'))->toArray(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @Groups({"map"}) |
102
|
|
|
*/ |
103
|
|
|
public function bench_map_getter_closure() |
104
|
|
|
{ |
105
|
|
|
return (new ArrayStream($this->data))->map(function ($e) { return $e->sub(); })->toArray(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @Groups({"sort"}) |
110
|
|
|
*/ |
111
|
|
|
public function bench_sort_array_stream() |
112
|
|
|
{ |
113
|
|
|
return iterator_to_array((new ArrayStream($this->data))->sort()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @Groups({"sort"}) |
118
|
|
|
*/ |
119
|
|
|
public function bench_sort_mutable_array_stream() |
120
|
|
|
{ |
121
|
|
|
return iterator_to_array((new MutableArrayStream($this->data))->sort()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @Groups({"sort"}) |
126
|
|
|
*/ |
127
|
|
|
public function bench_sort_array_iterator() |
128
|
|
|
{ |
129
|
|
|
$data = new \ArrayIterator($this->data); |
130
|
|
|
$data->asort(); |
131
|
|
|
|
132
|
|
|
return iterator_to_array($data); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
class MyEntity implements \Bdf\Collection\Util\Hashable |
137
|
|
|
{ |
138
|
|
|
public $id; |
139
|
|
|
public $sub; |
140
|
|
|
|
141
|
|
|
public function __construct($id, $sub = null) |
142
|
|
|
{ |
143
|
|
|
$this->id = $id; |
144
|
|
|
$this->sub = $sub; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return mixed |
149
|
|
|
*/ |
150
|
|
|
public function id() |
151
|
|
|
{ |
152
|
|
|
return $this->id; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @return MyEntity |
157
|
|
|
*/ |
158
|
|
|
public function sub() |
159
|
|
|
{ |
160
|
|
|
return $this->sub; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function hash() |
164
|
|
|
{ |
165
|
|
|
return $this->id; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|