Skip to content

Instantly share code, notes, and snippets.

@dongilbert
Created January 17, 2014 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dongilbert/8480612 to your computer and use it in GitHub Desktop.
Save dongilbert/8480612 to your computer and use it in GitHub Desktop.
Benchmark newInstanceWithoutConstructor vs unserialize for object creation
ReflectionClass::newInstanceWithoutConstructor: 0.039168834686279
unserialize: 0.14324808120728
<?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";
@danapplegate
Copy link

Thanks for the analysis! Mind if I link to this and mention it in the post?

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