Skip to content

Instantly share code, notes, and snippets.

@brannow
Last active May 7, 2018 15:38
Show Gist options
  • Save brannow/74dfb504af04d89e44845124497856a5 to your computer and use it in GitHub Desktop.
Save brannow/74dfb504af04d89e44845124497856a5 to your computer and use it in GitHub Desktop.
Performance lost form invalid array offests and indexes
<?php declare(strict_types=1);
// disable error logging
error_reporting(0);
// disable error display
ini_set("display_errors", '0');
function generateSmallNumericTestArray(): array
{
$ta = [];
for ($i = 0; $i < 5; ++$i) {
$ta[] = $i;
}
return $ta;
}
function generateSmallNumericAssocTestArray(): array
{
$ta = [];
for ($i = 0; $i < 5; ++$i) {
// ascii offset for uppercase letter
$ta[chr($i + 65)] = chr($i + 65);
}
return $ta;
}
$testArrayNumeric = generateSmallNumericTestArray();
/*
array(5) {
[0] =>
int(0)
[1] =>
int(1)
[2] =>
int(2)
[3] =>
int(3)
[4] =>
int(4)
}
*/
$testArrayAssoc = generateSmallNumericAssocTestArray();
/*
array(5) {
'A' =>
string(1) "A"
'B' =>
string(1) "B"
'C' =>
string(1) "C"
'D' =>
string(1) "D"
'E' =>
string(1) "E"
}
*/
echo PHP_EOL.PHP_EOL;
$c = 30;
$it = 100000;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if ($testArrayNumeric[3]) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayNumeric (valid) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (isset($testArrayNumeric[3])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayNumeric (valid + check) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (!empty($testArrayNumeric[3])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayNumeric (valid + check(empty)) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if ($testArrayNumeric[7]) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayNumeric (invalid) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (isset($testArrayNumeric[7])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayNumeric (invalid + check) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (!empty($testArrayNumeric[7])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayNumeric (invalid + check(empty)) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if ($testArrayAssoc['C']) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayAssoc (valid) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (isset($testArrayAssoc['C'])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayAssoc (valid + check) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (!empty($testArrayAssoc['C'])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayAssoc (valid + check(empty)) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if ($testArrayAssoc['Z']) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayAssoc (invalid) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (isset($testArrayAssoc['Z'])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayAssoc (invalid + check) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
$r = 0;
for ($a = 0; $a < $c; ++$a) {
$t = microtime(true);
for ($i = 0; $i < $it; ++$i) {
if (!empty($testArrayAssoc['Z'])) {}
}
$r += microtime(true) - $t;
}
echo 'testArrayAssoc (invalid + check(empty)) ('. $it .' * '. $c .') avg: ' . ($r / $c) . PHP_EOL;
/*
testArrayNumeric (valid) (100000 * 30) avg: 0.012220096588135
testArrayNumeric (valid + check) (100000 * 30) avg: 0.01052413781484
testArrayNumeric (valid + check(empty)) (100000 * 30) avg: 0.01347184975942
testArrayNumeric (invalid) (100000 * 30) avg: 0.055137117703756
testArrayNumeric (invalid + check) (100000 * 30) avg: 0.010439761479696
testArrayNumeric (invalid + check(empty)) (100000 * 30) avg: 0.012797570228577
testArrayAssoc (valid) (100000 * 30) avg: 0.012551267941793
testArrayAssoc (valid + check) (100000 * 30) avg: 0.011203114191691
testArrayAssoc (valid + check(empty)) (100000 * 30) avg: 0.01309986114502
testArrayAssoc (invalid) (100000 * 30) avg: 0.055405537287394
testArrayAssoc (invalid + check) (100000 * 30) avg: 0.010431138674418
testArrayAssoc (invalid + check(empty)) (100000 * 30) avg: 0.013095823923747
*/
echo PHP_EOL.PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment