forked from lucatume/di52
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-constructor.php
61 lines (53 loc) · 1.64 KB
/
test-constructor.php
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
<?php
class Engine
{
}
class Car
{
public function __construct($one, array $two = [], Engine $three = null, $four=23)
{
}
}
$epochs = 100000;
$m = new ReflectionMethod(Car::class, '__construct');
//// OO way.
$epoch = 0;
echo str_pad("\nOO ...",20);
$oostart = microtime(true);
while ($epoch++ < $epochs) {
foreach ($m->getParameters() as $i => $parameter) {
// Parameter #0 [ <optional> $one = 23 ]
// Parameter #1 [ <optional> array $two = Array ]
// Parameter #2 [ <optional> Engine or NULL $three = NULL ]
$string = $parameter->__toString();
$s = trim(str_replace('Parameter #' . $i, '', $string), '[ ]');
$frags = explode(' ', $s);
$equalsIndex = array_search('=', $frags);
$isOptional = $frags[0] === '<optional>';
$paramData = [
'class' => strpos($frags[1], '$') === 0 ? null : $frags[1],
'isOptional' => $isOptional,
'defaultValue' => $isOptional ? $parameter->getDefaultValue() : null,
];
}
}
$ootime = microtime(true) - $oostart;
echo $ootime;
////
//// toString way.
$epoch = 0;
echo str_pad("\n__toString ...",20);
$toStringStart = microtime(true);
while ($epoch++ < $epochs) {
foreach ($m->getParameters() as $parameter) {
$paramData = [
'class' => $parameter->getClass() ?: null,
'isOptional' => $parameter->isOptional(),
'defaultValue' => $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null,
];
}
}
$toStringtime = microtime(true) - $toStringStart;
echo $toStringtime;
////
echo "\nOO_time / TS_time = " . ($ootime / $toStringtime);