Skip to content

Instantly share code, notes, and snippets.

@sleekslush
Created July 5, 2012 15:00
Show Gist options
  • Save sleekslush/3054196 to your computer and use it in GitHub Desktop.
Save sleekslush/3054196 to your computer and use it in GitHub Desktop.
Benchmark PHP object creation
<?php
function benchmark($description, $fn, $iterations = 10000000) {
$start = microtime(true);
for ($i = 0; $i < $iterations; $i++) {
$fn();
}
$elapsed = microtime(true) - $start;
echo "Elapsed time ({$description}): {$elapsed}\n";
}
benchmark('new stdClass', function() {
$o = new stdClass();
});
benchmark('cast null to object', function() {
$o = (object) null;
});
[sleekslush ~]$ php object_benchmark.php
Elapsed time (new stdClass): 4.9088141918182
Elapsed time (cast null to object): 3.5254530906677
[sleekslush ~]$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment