Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Last active August 29, 2015 14:00
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dhrrgn/11374726 to your computer and use it in GitHub Desktop.
Save dhrrgn/11374726 to your computer and use it in GitHub Desktop.
Handy Benchmarking Function
<?php
function benchmark($name, $iterations, Closure $function)
{
echo "Starting Benchmark: {$name} (".number_format($iterations)." Iterations)\n";
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$function();
}
$elapsed = microtime(true) - $start;
$elapsedMean = number_format(($elapsed / $iterations) * 1000, 4);
$elapsedTotal = number_format($elapsed, 4);
echo " Total Time: {$elapsedTotal}s\n";
echo " Mean Time: {$elapsedMean}ms\n\n";
}
<?php
namespace Foo\Bar {
class Baz {}
}
namespace {
include 'benchmark.php';
$iters = 100000;
$testCls = new Foo\Bar\Baz();
benchmark('Reflection-Based', $iters, function () use ($testCls) {
$name = (new ReflectionClass($testCls))->getShortName();
});
benchmark('get_class-Based', $iters, function () use ($testCls) {
$nsParts = explode('\\', get_class($testCls));
$name = reset($nsParts);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment