Skip to content

Instantly share code, notes, and snippets.

@borkweb
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save borkweb/10739054 to your computer and use it in GitHub Desktop.
Save borkweb/10739054 to your computer and use it in GitHub Desktop.
<?php
$loops = 1000000;
$start = microtime( TRUE );
for ( $i = 0; $i < $loops; $i++ ) {
$thing = (object) array(
'cat' => (object) array(
'name' => 'Mr. Giggles',
'age' => 3,
),
'door' => (object) array(
'age' => 1,
),
);
}//end for
$total = microtime( TRUE ) - $start;
$avg = $total / $loops;
echo "Average for casting: {$total}s ( {$avg}s )\n";
$start = microtime( TRUE );
for ( $i = 0; $i < $loops; $i++ ) {
$thing = '{ "cat": { "name": "Mr. Giggles", "age": 3 }, "door" : { "age": 1 } }';
json_decode( $thing );
}//end for
$total = microtime( TRUE ) - $start;
$avg = $total / $loops;
echo "Average for json_decode: {$total}s ( {$avg}s )\n";
$start = microtime( TRUE );
for ( $i = 0; $i < $loops; $i++ ) {
$thing = new stdClass;
$thing->cat = new stdClass;
$thing->cat->name = 'Mr. Giggles';
$thing->cat->age = 3;
$thing->door = new stdClass;
$thing->door->age = 1;
}//end for
$total = microtime( TRUE ) - $start;
$avg = $total / $loops;
echo "Average for stdClass: {$total}s ( {$avg}s )\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment