Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP stdClass initialization vs array-to-object typecast
<?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";
$ 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
You can’t perform that action at this time.