Created
March 30, 2014 06:01
-
-
Save mtdowling/9868295 to your computer and use it in GitHub Desktop.
Array and object truthiness
This file contains 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 | |
@$i = $argv[1] ?: 1000; | |
$c = array(); | |
$s = microtime(true); | |
for ($j = 0; $j < $i; $j++) { | |
if ($c) {} | |
} | |
echo 'Empty Array: ' . (microtime(true) - $s) . "\n"; | |
$c = range(0, 10000); | |
$s = microtime(true); | |
for ($j = 0; $j < $i; $j++) { | |
if ($c) {} | |
} | |
echo 'Large Array: ' . (microtime(true) - $s) . "\n"; | |
$c = range(0, 10000); | |
$s = microtime(true); | |
for ($j = 0; $j < $i; $j++) { | |
if (empty($c)) {} | |
} | |
echo 'Lg w/Empty(): ' . (microtime(true) - $s) . "\n"; | |
$obj = new SplObjectStorage(); | |
for ($i = 0; $i < 1000; $i++) { | |
$obj->attach(new SplObjectStorage()); | |
} | |
$s = microtime(true); | |
for ($j = 0; $j < $i; $j++) { | |
if ($obj) {} | |
} | |
echo 'Large Object: ' . (microtime(true) - $s) . "\n"; | |
$s = microtime(true); | |
for ($j = 0; $j < $i; $j++) { | |
if (count($c) === 0) {} | |
} | |
echo 'Using count: ' . (microtime(true) - $s) . "\n"; |
Author
mtdowling
commented
Mar 30, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment