note 101882 added to function.count
| From: | pro at developergatsby dot com | Date: | Sat, 15 Jan 2011 22:37:41 +0000 |
| Subject: | note 101882 added to function.count | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-175792@lists.php.net to get a copy of this message | ||
Complexity of count function.
I just tested the complexity of count().
It looks O(1).
Here is the result.
<< counting array with 1 element>>
total time : 0.021ms
start -> end : 100 %
<< counting array with 100000 elements>>
total time : 0.015ms
start -> end : 100 %
Here is the testing code.
mini_bench_to is a function.
<?php
$arr = array();
$arr[] = "1";
$t['start'] = microtime(true);
$size = count($arr);
$t['end'] = microtime(true);
$str_result_bench = mini_bench_to($t);
echo $str_result_bench; // string return
$arr = array();
for($index = 0; $index < 100000; $index++) {
$arr[] = $index;
}
$t['start'] = microtime(true);
$size = count($arr);
$t['end'] = microtime(true);
$str_result_bench=mini_bench_to($t);
echo $str_result_bench; // string return
?>
<?php
function mini_bench_to($arg_t, $arg_ra=false) {
$tttime=round((end($arg_t)-$arg_t['start'])*1000,4);
if ($arg_ra) $ar_aff['total_time']=$tttime;
else $aff="total time : ".$tttime."ms\n";
$prv_cle='start';
$prv_val=$arg_t['start'];
foreach ($arg_t as $cle=>$val)
{
if($cle!='start')
{
$prcnt_t=round(((round(($val-$prv_val)*1000,4)/$tttime)*100),1);
if ($arg_ra) $ar_aff[$prv_cle.' -> '.$cle]=$prcnt_t;
$aff.=$prv_cle.' -> '.$cle.' : '.$prcnt_t." %\n";
$prv_val=$val;
$prv_cle=$cle;
}
}
if ($arg_ra) return $ar_aff;
return $aff;
}
?>
----
Server IP: 208.69.120.55
Probable Submitter: 75.15.16.9
----
Manual Page -- http://www.php.net/manual/en/function.count.php
Edit -- https://master.php.net/note/edit/101882
Del: integrated -- https://master.php.net/note/delete/101882/integrated
Del: useless -- https://master.php.net/note/delete/101882/useless
Del: bad code -- https://master.php.net/note/delete/101882/bad+code
Del: spam -- https://master.php.net/note/delete/101882/spam
Del: non-english -- https://master.php.net/note/delete/101882/non-english
Del: in docs -- https://master.php.net/note/delete/101882/in+docs
Del: other reasons-- https://master.php.net/note/delete/101882
Reject -- https://master.php.net/note/reject/101882
Search -- https://master.php.net/manage/user-notes.php