Skip to content

Instantly share code, notes, and snippets.

@mtdowling
Created March 30, 2014 06:01
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 mtdowling/9868295 to your computer and use it in GitHub Desktop.
Save mtdowling/9868295 to your computer and use it in GitHub Desktop.
Array and object truthiness
<?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";
@mtdowling
Copy link
Author

$ php ~/projects/perf/if_checks.php 1000
Empty Array:  0.0003509521484375
Large Array:  0.00031280517578125
Lg w/Empty(): 0.00031495094299316
Large Object: 0.00024008750915527
Using count:  0.0012218952178955

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment