note 123719 added to function.empty
| From: | markmanning at gmail dot com | Date: | Sat, 23 Mar 2019 09:15:46 +0000 |
| Subject: | note 123719 added to function.empty | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210426@lists.php.net to get a copy of this message | ||
I normally count() an array, so I wanted to see how empty() would stack up.
<?php
$test = array();
$test2 = array();
for ($x = 0; $x < 1000; $x++) $test[] = $x;
$ts = microtime(true);
for ($x = 0; $x < 100000000; $x++)
{
if (count($test))
{
}
}
echo "Time taken: " . (microtime(true) - $ts) . " sec\n";
?>
For 100,000,000 comparisons, here are the results against PHP 7.2.16 on my hardware:
count($test): 2.697 sec
count($test2): 2.596 sec
$test === array(): 2.579 sec
$test2 === array(): 2.552 sec
empty($test): 3.085 sec
empty($test2): 3.113 sec
In short, it doesn't matter what method is used although empty() is actually just ever so
slightly slower despite it being a language construct. YMMV.
----
Server IP: 216.194.115.244
Probable Submitter: 174.18.83.153
----
Manual Page -- http://php.net/manual/en/function.empty.php
Edit -- https://master.php.net/note/edit/123719
Del: integrated -- https://master.php.net/note/delete/123719/integrated
Del: useless -- https://master.php.net/note/delete/123719/useless
Del: bad code -- https://master.php.net/note/delete/123719/bad+code
Del: spam -- https://master.php.net/note/delete/123719/spam
Del: non-english -- https://master.php.net/note/delete/123719/non-english
Del: in docs -- https://master.php.net/note/delete/123719/in+docs
Del: other reasons-- https://master.php.net/note/delete/123719
Reject -- https://master.php.net/note/reject/123719
Search -- https://master.php.net/manage/user-notes.php