Skip to content

Commit 9019505

Browse files
committed
better bench for Enum::[get|detect]Constants()
1 parent 858b9f7 commit 9019505

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

bench/EnumBench.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace MabeEnumBench;
44

5+
use MabeEnum\Enum;
56
use MabeEnumTest\TestAsset\Enum66;
7+
use ReflectionClass;
8+
use ReflectionProperty;
69

710
/**
811
* Benchmark of abstract class Enum tested with enumeration of 66 enumerators.
@@ -17,6 +20,10 @@
1720
*/
1821
class EnumBench
1922
{
23+
/**
24+
* @var ReflectionProperty[]
25+
*/
26+
private $enumPropsRefl;
2027
/**
2128
* @var string[]
2229
*/
@@ -42,12 +49,25 @@ class EnumBench
4249
*/
4350
public function init()
4451
{
52+
$enumRefl = new ReflectionClass(Enum::class);
53+
$this->enumPropsRefl = $enumRefl->getProperties(ReflectionProperty::IS_STATIC);
54+
foreach ($this->enumPropsRefl as $enumPropRefl) {
55+
$enumPropRefl->setAccessible(true);
56+
}
57+
4558
$this->names = Enum66::getNames();
4659
$this->values = Enum66::getValues();
4760
$this->ordinals = Enum66::getOrdinals();
4861
$this->enumerators = Enum66::getEnumerators();
4962
}
5063

64+
private function resetStaticEnumProps()
65+
{
66+
foreach ($this->enumPropsRefl as $enumPropRefl) {
67+
$enumPropRefl->setValue([]);
68+
}
69+
}
70+
5171
public function benchGetName()
5272
{
5373
foreach ($this->enumerators as $enumerator) {
@@ -85,6 +105,7 @@ public function benchIsByValue()
85105

86106
public function benchGetConstants()
87107
{
108+
$this->resetStaticEnumProps();
88109
Enum66::getConstants();
89110
}
90111

0 commit comments

Comments
 (0)