Created
September 28, 2011 13:04
-
-
Save thiagophx/1247878 to your computer and use it in GitHub Desktop.
Object creation in PHP, which approach is faster? new, clone or unserialize???
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 | |
function leSerialize() | |
{ | |
for ($i = 0; $i < 1000; $i++) | |
unserialize('O:8:"stdClass":0:{}'); | |
} | |
function leNew() | |
{ | |
for ($i = 0; $i < 1000; $i++) | |
new stdClass(); | |
} | |
function leClone() | |
{ | |
$a = new stdClass(); | |
for ($i = 0; $i < 1000; $i++) | |
clone $a; | |
} | |
leSerialize(); | |
leNew(); | |
leClone(); |
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 | |
function leSerialize() | |
{ | |
for ($i = 0; $i < 1000; $i++) | |
unserialize('O:8:"stdClass":0:{}'); | |
} | |
function leNew() | |
{ | |
for ($i = 0; $i < 1000; $i++) | |
new stdClass(); | |
} | |
function leClone() | |
{ | |
$a = new stdClass(); | |
for ($i = 0; $i < 1000; $i++) | |
clone $a; | |
} | |
function leJson() | |
{ | |
$json = '{}'; | |
for ($i = 0; $i < 1000; $i++) | |
json_decode($json); | |
} | |
function leReflection() | |
{ | |
$class = new ReflectionClass('stdClass'); | |
for ($i = 0; $i < 1000; $i++) | |
$class->newInstance(); | |
} | |
leSerialize(); | |
leNew(); | |
leClone(); | |
leJson(); | |
leReflection(); |
i'm burn.
Cool, our job discuss... maybe we can use reflection, json_decode and others too
http://p.twimg.com/AaeNEycCQAEdG3l.png:large New benchmark image
You should run the tests in separate processes.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://p.twimg.com/AablsOcCQAAnjnv.png:large Image of the benchmark results