note 101882 deleted from function.count by googleguy
| From: | googleguy@php.net | Date: | Mon, 17 Sep 2012 00:08:21 +0000 |
| Subject: | note 101882 deleted from function.count by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-190823@lists.php.net to get a copy of this message | ||
Note Submitter: pro at developergatsby dot com
----
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;
}
?>