Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hasyimibhar/691983fe65d4f0eee5a1 to your computer and use it in GitHub Desktop.
Save hasyimibhar/691983fe65d4f0eee5a1 to your computer and use it in GitHub Desktop.
Benchmark of calling ReflectionMethod::getParameters
<?php
ini_set('memory_limit', '-1');
$n = 1000000;
class A {}
class B {}
class C {}
class D {}
class E {}
class F {}
class G {}
class H {}
class I {}
class J {}
class Foo
{
public function bar(A $a, B $b, C $c, D $d, E $e, F $f, G $g, H $h, I $i, J $j)
{
// ..
}
}
$foo = new Foo();
$times = [];
for ($i = 0; $i < $n; $i++) {
$start = microtime(true);
$ref = new ReflectionMethod($foo, 'bar');
$params = $ref->getParameters();
$end = microtime(true);
$times[] = $end - $start;
}
$avg = array_sum($times) / $n;
echo "Average of $n: " . number_format($avg, 10) . " sec, or " . number_format($avg * 1000000, 10) . " usec\n";
Average of 1000000: 0.0000090445 sec, or 9.0445024967 usec
@hasyimibhar
Copy link
Author

Sample benchmark ran on:

PHP 5.6.16 (cli) (built: Nov 27 2015 10:28:34)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment