note 124215 added to function.count
| From: | jerome dot gds at gmail dot com | Date: | Fri, 20 Sep 2019 12:53:19 +0000 |
| Subject: | note 124215 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-212257@lists.php.net to get a copy of this message | ||
to end the debate: count() is the same as empty()
test code below:
results on my computer:
count : double(0.81396999359131)
empty : double(0.81621310710907)
using isset($test[0]) is a bit slower than empty;
test without adding value to the array in function ****Test: still the same.
<?php
function average(array $test)
{
$sum = 0;
foreach($test as $value) {
$sum += $value;
}
return $sum;
}
function countTest(array $test)
{
$i = 0;
while ($i++ < 1000000) {
count($test);
$test["lol$i"] = "teset$i";
}
}
function emptyTest(array $test)
{
$i = 0;
while ($i++ < 1000000) {
empty($test);
$test["lol$i"] = "teset$i";
}
}
$test = [];
$i = 0;
while ($i++ < 20000000) {
$test[] = $i;
}
$j = 10;
$value = $j;
$count = [];
$isEmpty = [];
while ($j--) {
$time = microtime(true);
countTest($test);
$count[] = microtime(true) - $time;
$time = microtime(true);
emptyTest($test);
$isEmpty[] = microtime(true) - $time;
}
var_dump(average($count) / $value);
var_dump(average($isEmpty) / $value);
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.168 (proxied: 195.248.251.18)
----
Manual Page -- http://php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/124215
Del: integrated -- https://master.php.net/note/delete/124215/integrated
Del: useless -- https://master.php.net/note/delete/124215/useless
Del: bad code -- https://master.php.net/note/delete/124215/bad+code
Del: spam -- https://master.php.net/note/delete/124215/spam
Del: non-english -- https://master.php.net/note/delete/124215/non-english
Del: in docs -- https://master.php.net/note/delete/124215/in+docs
Del: other reasons-- https://master.php.net/note/delete/124215
Reject -- https://master.php.net/note/reject/124215
Search -- https://master.php.net/manage/user-notes.php