public

__call versus reflection

  • Download Gist
call.versus.reflection.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
<?php
 
/**
* A Simple Operation Benchmark Class
*
* @author Tyler Sommer
*/
class Benchmark {
protected $_length;
protected $_tests = array();
protected $_results = array();
 
/**
* @param int $length The number of iterations per test
*/
public function __construct($length = 1000) {
$this->_length = $length;
}
/**
* Register a test
*
* @param string $name (Friendly) Name of the test
* @param callback $callback A valid callback
*/
public function register($name, $callback) {
$this->_tests[$name] = $callback;
}
 
/**
* Execute the registered tests and display the results
*/
public function execute() {
$adjustment = round($this->_length * .1, 0);
echo "Running " . count($this->_tests) . " tests, {$this->_length} times each...\nThe {$adjustment} highest and lowest results will be disregarded.\n\n";
foreach ($this->_tests as $name => $test) {
$results = array();
for ($x = 0; $x < $this->_length; $x++) {
ob_start();
$start = time() + microtime();
call_user_func($test);
$results[] = round((time() + microtime()) - $start, 10);
ob_end_clean();
}
sort($results);
// remove the lowest and highest 10% (leaving 80% of results)
for ($x = 0; $x < $adjustment; $x++) {
array_shift($results);
array_pop($results);
}
$avg = array_sum($results) / count($results);
echo "For {$name}, out of " . count($results) . " runs, average time was: " . sprintf("%.10f", $avg) . " secs.\n";
$this->_results[$name] = $avg;
}
asort($this->_results);
reset($this->_results);
$fastestResult = each($this->_results);
reset($this->_results);
echo "\n\nResults:\n";
printf("%-25s\t%-20s\t%s\n", "Test Name", "Time", "+ Interval");
foreach ($this->_results as $name => $result) {
$interval = $result - $fastestResult["value"];
printf("%-25s\t%-20s\t%s\n", $name, sprintf("%.10f", $result), "+" . sprintf("%.10f", $interval));
}
}
}
 
class TestClass
{
protected function myMethod()
{
return true;
}
public function __call($method, $args)
{
return call_user_func_array(array($this, $method), $args);
}
public function myPublicMethod()
{
return true;
}
}
 
$bm = new Benchmark(25000);
$bm->register("reflection", function() {
$obj = new TestClass();
$reflected = new ReflectionClass('TestClass');
$method = $reflected->getMethod('myMethod');
$method->setAccessible(true);
$result = $method->invoke($obj);
});
$bm->register("__call", function() {
$obj = new TestClass();
$result = $obj->myMethod();
});
$bm->register("public", function() {
$obj = new TestClass();
$result = $obj->myPublicMethod();
});
$bm->execute();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.