Created
May 9, 2019 07:49
-
-
Save mxr576/79c9ea74cf5f3991805e7861c1247756 to your computer and use it in GitHub Desktop.
is_a() vs. reflection benchmark
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$cnt = 10000000; | |
interface FooInterface {} | |
class BarClass implements FooInterface { | |
} | |
$x = 0; | |
$start = microtime(TRUE); | |
for ($i = 0; $i < $cnt; $i++) { | |
if (is_a(BarClass::class, FooInterface::class, TRUE)) { | |
$x++; | |
} | |
} | |
$end = microtime(TRUE); | |
echo 'Time: ' . ($end - $start) . " ms" . PHP_EOL; | |
$x = 0; | |
$start = microtime(TRUE); | |
$rc = new ReflectionClass(BarClass::class); | |
for ($i = 0; $i < $cnt; $i++) { | |
if ($rc->implementsInterface(FooInterface::class)) { | |
$x++; | |
} | |
} | |
$end = microtime(TRUE); | |
echo 'Time: ' . ($end - $start) . " ms" . PHP_EOL; | |
$x = 0; | |
$start = microtime(TRUE); | |
for ($i = 0; $i < $cnt; $i++) { | |
$rc = new ReflectionClass(BarClass::class); | |
if ($rc->implementsInterface(FooInterface::class)) { | |
$x++; | |
} | |
} | |
$end = microtime(TRUE); | |
echo 'Time: ' . ($end - $start) . " ms" . PHP_EOL; | |
# End of speedtest.php |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment