Created
February 24, 2016 10:41
-
-
Save jonaz/506cdb6c0308300c23b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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