Created
July 5, 2012 15:00
-
-
Save sleekslush/3054196 to your computer and use it in GitHub Desktop.
Benchmark PHP object creation
This file contains hidden or 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 | |
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; | |
}); |
This file contains hidden or 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
[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