Skip to content

Instantly share code, notes, and snippets.

@mxr576
Created May 9, 2019 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mxr576/79c9ea74cf5f3991805e7861c1247756 to your computer and use it in GitHub Desktop.
Save mxr576/79c9ea74cf5f3991805e7861c1247756 to your computer and use it in GitHub Desktop.
is_a() vs. reflection benchmark
<?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