<?php
namespace Foo\Bar {
class Baz {}
}
namespace {
function byReflection($obj) {
$r = new ReflectionObject($obj);
return $r->getShortName();
}
function byString($obj) {
$class = get_class($obj);
return ltrim(strrchr($class, '\\'), '\\') ?: $class;
}
function benchmark(callable $func, $param, $message) {
echo "Benchmarking $message\n";
$s = microtime(true);
for ($i = 0; $i < 10000; $i++) {
$func($param);
}
$e = microtime(true);
echo " - Completed in " . ($e - $s) . " seconds\n";
}
$a = new StdClass;
$b = new Foo\Bar\Baz;
benchmark("byReflection", $a, "By Reflection, No Namespace");
benchmark("byReflection", $b, "By Reflection, Namespace");
benchmark("byString", $a, "By String, No Namespace");
benchmark("byString", $b, "By String, Namespace");
}
preferences:
45.86 ms | 402 KiB | 5 Q