1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
use Innmind\Immutable\Map; |
5
|
|
|
|
6
|
|
|
final class MapPrimitiveBench |
7
|
|
|
{ |
8
|
|
|
private $data; |
9
|
|
|
private $map; |
10
|
|
|
|
11
|
|
|
public function __construct() |
12
|
|
|
{ |
13
|
|
|
$this->data = unserialize(file_get_contents(__DIR__.'/fixtures.data')); |
14
|
|
|
$this->map = Map::of( |
15
|
|
|
'int', |
16
|
|
|
'int', |
17
|
|
|
array_keys($this->data), |
18
|
|
|
array_values($this->data) |
19
|
|
|
); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function benchNamedConstructor() |
23
|
|
|
{ |
24
|
|
|
Map::of( |
25
|
|
|
'int', |
26
|
|
|
'int', |
27
|
|
|
array_keys($this->data), |
28
|
|
|
array_values($this->data) |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function benchGet() |
33
|
|
|
{ |
34
|
|
|
$this->map->get(500); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function benchContains() |
38
|
|
|
{ |
39
|
|
|
$this->map->contains(500); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function benchEquals() |
43
|
|
|
{ |
44
|
|
|
$this->map->equals($this->map); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function benchFilter() |
48
|
|
|
{ |
49
|
|
|
$this->map->filter(static function(int $k, int $v): bool { |
50
|
|
|
return $v % 2 === 0; |
51
|
|
|
}); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function benchForeach() |
55
|
|
|
{ |
56
|
|
|
$this->map->foreach(static function(int $k, int $v): void { |
|
|
|
|
57
|
|
|
// pass |
58
|
|
|
}); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function benchGroupBy() |
62
|
|
|
{ |
63
|
|
|
$this->map->groupBy(static function(int $k, int $v): int { |
64
|
|
|
return $v % 2; |
65
|
|
|
}); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function benchKeys() |
69
|
|
|
{ |
70
|
|
|
$this->map->keys(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function benchValues() |
74
|
|
|
{ |
75
|
|
|
$this->map->values(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function benchMap() |
79
|
|
|
{ |
80
|
|
|
$this->map->map(static function(int $i): int { |
81
|
|
|
return $i ** 2; |
82
|
|
|
}); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function benchRemove() |
86
|
|
|
{ |
87
|
|
|
$this->map->remove(500); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function benchMerge() |
91
|
|
|
{ |
92
|
|
|
$this->map->merge($this->map); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function benchPartition() |
96
|
|
|
{ |
97
|
|
|
$this->map->partition(static function(int $k, int $v): bool { |
98
|
|
|
return $v % 2 === 0; |
99
|
|
|
}); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function benchReduce() |
103
|
|
|
{ |
104
|
|
|
$this->map->reduce( |
105
|
|
|
0, |
106
|
|
|
static function(int $sump, int $k, int $v): int { |
107
|
|
|
return $sum + $k + $v; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.