File: EnumMapBench.php

package info (click to toggle)
php-enum 4.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 556 kB
  • sloc: php: 3,489; xml: 15; makefile: 8
file content (165 lines) | stat: -rw-r--r-- 3,501 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php

namespace MabeEnumBench;

use MabeEnum\EnumMap;
use MabeEnumTest\TestAsset\Enum66;

/**
 * Benchmark of EnumMap used with an enumeration of 66 enumerators.
 *
 * @BeforeMethods({"init"})
 * @Revs(2000)
 * @Iterations(25)
 *
 * @link http://github.com/marc-mabe/php-enum for the canonical source repository
 * @copyright 2020, Marc Bennewitz
 * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
 */
class EnumMapBench
{
    /**
     * @var mixed[]
     */
    private $values;

    /**
     * @var Enum66[]
     */
    private $enumerators;

    /**
     * @var EnumMap
     */
    private $emptyMap;

    /**
     * @var EnumMap
     */
    private $fullMap;

    /**
     * Will be called before every subject
     */
    public function init()
    {
        $this->values      = Enum66::getValues();
        $this->enumerators = Enum66::getEnumerators();

        $this->emptyMap = new EnumMap(Enum66::class);
        $this->fullMap  = new EnumMap(Enum66::class);
        foreach ($this->enumerators as $i => $enumerator) {
            $this->fullMap->offsetSet($enumerator, $i);
        }
    }

    public function benchGetKeysEmpty()
    {
        $this->emptyMap->getKeys();
    }

    public function benchGetKeysFull()
    {
        $this->fullMap->getKeys();
    }

    public function benchGetValuesEmpty()
    {
        $this->emptyMap->getValues();
    }

    public function benchGetValuesFull()
    {
        $this->fullMap->getValues();
    }

    public function benchSearchTypeJuggling()
    {
        $this->fullMap->search('31');
    }

    public function benchSearchStrict()
    {
        $this->fullMap->search(31, true);
    }

    public function benchOffsetSetEnumerator()
    {
        foreach ($this->enumerators as $enumerator) {
            $this->emptyMap->offsetSet($enumerator);
        }
    }

    public function benchOffsetSetValue()
    {
        foreach ($this->values as $value) {
            $this->emptyMap->offsetSet($value);
        }
    }

    public function benchOffsetUnsetEnumerator()
    {
        foreach ($this->enumerators as $enumerator) {
            $this->fullMap->offsetUnset($enumerator);
        }
    }

    public function benchOffsetUnsetValue()
    {
        foreach ($this->values as $value) {
            $this->fullMap->offsetUnset($value);
        }
    }

    public function benchOffsetExistsEnumerator()
    {
        foreach ($this->enumerators as $enumerator) {
            $this->fullMap->offsetExists($enumerator);
        }
    }

    public function benchOffsetExistsValue()
    {
        foreach ($this->values as $value) {
            $this->fullMap->offsetExists($value);
        }
    }

    public function benchHasEnumerator()
    {
        foreach ($this->enumerators as $enumerator) {
            $this->fullMap->has($enumerator);
        }
    }

    public function benchHasValue()
    {
        foreach ($this->values as $value) {
            $this->fullMap->has($value);
        }
    }

    public function benchIterateFull()
    {
        foreach ($this->fullMap as $enumerator => $_) {
            $enumerator->getValue();
        }
    }

    public function benchIterateEmpty()
    {
        foreach ($this->emptyMap as $enumerator => $_) {
            $enumerator->getValue();
        }
    }

    public function benchCountFull()
    {
        $this->fullMap->count();
    }

    public function benchCountEmpty()
    {
        $this->emptyMap->count();
    }
}