note 128069 added to function.empty
| From: | mmulej at gmail dot com | Date: | Fri, 30 Dec 2022 11:28:54 +0000 |
| Subject: | note 128069 added to function.empty | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220024@lists.php.net to get a copy of this message | ||
Tests on
- Win 11
- Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz 2.21 GHz
- 64Gb RAM,
- PHP 8.1.10 (in Tinkerwell)
repeatedly show
empty > count > === [], but the margin
is small and not worth the micro-optimizations.
Note: Same results (with $n = 10000000) for PHP v7.4.33 using onlinephp(dot)io sandbox
tool.
<?php
$test = range(0, 1000);
$test2 = [];
$n = 1000_000_000;
$t1 = microtime(true); for ($x = 0; $x < $n; $x++) if (count($test)) {}
$t2 = microtime(true); for ($x = 0; $x < $n; $x++) if (count($test2)) {}
$t3 = microtime(true); for ($x = 0; $x < $n; $x++) if ($test === []) {}
$t4 = microtime(true); for ($x = 0; $x < $n; $x++) if ($test2 === []) {}
$t5 = microtime(true); for ($x = 0; $x < $n; $x++) if (empty($test)) {}
$t6 = microtime(true); for ($x = 0; $x < $n; $x++) if (empty($test2)) {}
$t7 = microtime(true);
echo "count(\$test): " . ($t2 - $t1) . " sec\n";
echo "count(\$test2): " . ($t3 - $t2) . " sec\n";
echo "\$test === []: " . ($t4 - $t3) . " sec\n";
echo "\$test2 === []: " . ($t5 - $t4) . " sec\n";
echo "empty(\$test): " . ($t6 - $t5) . " sec\n";
echo "empty(\$test2): " . ($t7 - $t6) . " sec\n";
?>
Results:
<?php
count($test): 18.034885168076 sec
count($test2): 17.133869886398 sec
$test === []: 20.059770107269 sec
$test2 === []: 14.204195022583 sec
empty($test): 13.583840847015 sec
empty($test2): 12.971315145493 sec
?>
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.158.213 (proxied: 89.143.113.200)
----
Manual Page -- https://php.net/manual/en/function.empty.php
Edit -- https://main.php.net/note/edit/128069
Del: integrated -- https://main.php.net/note/delete/128069/integrated
Del: useless -- https://main.php.net/note/delete/128069/useless
Del: bad code -- https://main.php.net/note/delete/128069/bad+code
Del: spam -- https://main.php.net/note/delete/128069/spam
Del: non-english -- https://main.php.net/note/delete/128069/non-english
Del: in docs -- https://main.php.net/note/delete/128069/in+docs
Del: other reasons-- https://main.php.net/note/delete/128069
Reject -- https://main.php.net/note/reject/128069
Search -- https://main.php.net/manage/user-notes.php