Skip to content

Instantly share code, notes, and snippets.

@jonaz
Created February 24, 2016 10:41
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 jonaz/506cdb6c0308300c23b7 to your computer and use it in GitHub Desktop.
Save jonaz/506cdb6c0308300c23b7 to your computer and use it in GitHub Desktop.
<?php
// A simple test object
class testObj
{
private $id;
public function getId()
{
return $this->id;
}
public function setId($newId)
{
$this->id = $newId;
}
}
function benchmark($name,$callback){
$timeStart = microtime(true);
$callback();
$timeEnd = microtime(true);
$time = $timeEnd - $timeStart;
echo str_pad($name,30)."$time seconds\n";
}
benchmark('variables', function(){
for ($i = 0; $i < 100000; $i++) {
$a = "testObj";
$method = "setId";
$test1 = new $a;
$test1->$method(33);
}
});
benchmark('call_user_func_array', function(){
for ($i = 0; $i < 100000; $i++) {
$a = "testObj";
$method = "setId";
$test1 = new $a;
call_user_func_array([$test1,$method], [33]);
}
});
benchmark('reflection with call', function(){
for ($i = 0; $i < 100000; $i++) {
$a = "testObj";
$method = "setId";
$a = "testObj";
$test2 = new $a;
$reflectionMethod = new \ReflectionMethod($test2, $method);
$args = [33];
$reflectionMethod->invokeArgs($test2,$args);
}
});
benchmark('reflection without call', function(){
for ($i = 0; $i < 100000; $i++) {
$a = "testObj";
$method = "setId";
$a = "testObj";
$test2 = new $a;
$reflectionMethod = new \ReflectionMethod($test2, $method);
//$reflectionMethod->getParameters();
}
});
benchmark('reflection getpara call_user', function(){
for ($i = 0; $i < 100000; $i++) {
$a = "testObj";
$method = "setId";
$a = "testObj";
$test2 = new $a;
$reflectionMethod = new \ReflectionMethod($test2, $method);
$reflectionMethod->getParameters();
call_user_func_array([$test2,$reflectionMethod->getName()], [33]);
}
});
benchmark('reflection getpara invoke', function(){
for ($i = 0; $i < 100000; $i++) {
$a = "testObj";
$method = "setId";
$a = "testObj";
$test2 = new $a;
$reflectionMethod = new \ReflectionMethod($test2, $method);
$reflectionMethod->getParameters();
$args = [33];
$reflectionMethod->invokeArgs($test2,$args);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment