Created
October 3, 2013 08:02
-
-
Save keichan34/6806681 to your computer and use it in GitHub Desktop.
PHP stdClass initialization vs array-to-object typecast
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 | |
$start = microtime(true); | |
for($i=0;$i<1000000;$i++) { | |
$a = new stdClass(); | |
$a->hello = 'there'; | |
} | |
$end = microtime(true); | |
echo "new stdClass x 1,000,000: " . ($end - $start) . " ms\n"; | |
$start = microtime(true); | |
for($i=0;$i<1000000;$i++) { | |
$a = (object)array('hello' => 'there'); | |
} | |
$end = microtime(true); | |
echo "array => object x 1,000,000: " . ($end - $start) . " ms\n"; |
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 php_stdclass_init.php | |
new stdClass x 1,000,000: 0.7306969165802 ms | |
array => object x 1,000,000: 0.45129489898682 ms |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment