Skip to content

Instantly share code, notes, and snippets.

@keichan34
Created October 3, 2013 08:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keichan34/6806681 to your computer and use it in GitHub Desktop.
Save keichan34/6806681 to your computer and use it in GitHub Desktop.
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