Created
January 17, 2014 20:15
-
-
Save dongilbert/8480612 to your computer and use it in GitHub Desktop.
Benchmark newInstanceWithoutConstructor vs unserialize for object creation
This file contains hidden or 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
ReflectionClass::newInstanceWithoutConstructor: 0.039168834686279 | |
unserialize: 0.14324808120728 |
This file contains hidden or 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 | |
class Foo | |
{ | |
public function __construct() | |
{ | |
die(__METHOD__ . ' called. Fail.'); | |
} | |
} | |
echo 'ReflectionClass::newInstanceWithoutConstructor: '; | |
$r = new ReflectionClass('Foo'); | |
$start = microtime(true); | |
for($i=1;$i<100000;$i++){ | |
$r->newInstanceWithoutConstructor(); | |
} | |
$now = microtime(true); | |
echo ($now - $start) . "\n\n"; | |
echo 'unserialize: '; | |
$r = new ReflectionClass('Foo'); | |
$start = microtime(true); | |
for($i=1;$i<100000;$i++){ | |
unserialize('O:3:"Foo":0:{}'); | |
} | |
$now = microtime(true); | |
echo ($now - $start) . "\n\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the analysis! Mind if I link to this and mention it in the post?