PHP 循环迭代 代替 递归

set_time_limit(1200);
ini_set('memory_limit', '1024M');
class PerformanceTest
{
    private $time;
    private $memory;

    public function begin()
    {
        $this->time = $this->getTime();
        $this->memory = $this->getMemory();
    }

    public function end()
    {

        $this->time   = $this->getTime()   - $this->time;
        $this->time   = round($this->time,23);//在这里才能格式化时间
        $this->memory = $this->getMemory() - $this->memory;
        $this->memory = $this->convert($this->memory);
        echo "time:{$this->time}秒<br />";
        echo "memory:{$this->memory}<br />";
        return $this->time;
    }

    public function getSpentTime()
    {
        return $this->time;
    }    
    public function getTime() 
    {  
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }

    public function getMemory()
    {
        return memory_get_usage();
    }

    public function convert(int $size)
    { 
        if(!$size)
        {
            return '0 b';
        }
        $size = abs($size);
        $unit=array('b','kb','mb','gb','tb','pb');
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; 
    } 
    
    function buildData(int $len)
    {
        $ret = [];
        if($len>0)
        {
            for($i=0;$i<$len;$i++)
            {
                $ret[] = mt_rand(0,1000); 
            }
        }
        return $ret;
    }
    
    function buildRandomData(int $len,int $maxLimit=1,bool $isChangeLimit=false)
    {
        $ret = [];
        $arrElementNum = 0;
        if($len > 0)
        {
            for($i=0;$i<$len;$i++)
            {
                $limit = $maxLimit;
                $arrElementNum = mt_rand(1,5);
                if($limit)//最大维数
                {
                    $limit--;
                    if($isChangeLimit)//是否可变长度  false固定长  true 可变长
                    {
                        if(mt_rand(0,4))
                        {
                            $ret[] = self::buildRandomData($arrElementNum,$limit);
                        }
                        else
                        {
                            $ret[] = mt_rand(1,5);
                        }
                        
                    }
                    else
                    {
                        $ret[] = self::buildRandomData($arrElementNum,$limit);
                    }
                }
                else
                {
                    $ret[] = mt_rand(1,5);
                }
            }
        }
        return $ret;
    }
    
    function randomStr()
    {
        $len = mt_rand(0,1000000);
        $str = '';
        for($i=0;$i<$len;$i++)
        {
            $str .= chr(mt_rand(65,122));
        }
        return $str;
    }
    

}


function iterator($a)
{
    $c = [];
    $c['menu'] = [];
    foreach($a as $k=>$v)
    {
        $b = $v;
        
        
        if(!isset($c['menu'][$b[0]]))
        {
            //$b[0]['child']=[];
            $c['menu'][$b[0]][$b[0]]=[$b[0]];
            $c['menu'][$b[0]][$b[0]]['child'] = [];
        }
        $d = &$c['menu'][$b[0]][$b[0]]['child'];
        unset($b[0]);
        
        //$d = &$c['menu'];
        foreach($b as $m=>$n)
        {
            $t = $n;
            if(isset($d[$t]))
            {
                $d = &$d[$t]['child'];//存在就引用它的child 并往下循环目录
                continue;
            }
            $d[$t] = [];
            //$n->value = 'bb';
            
            //$n->value = 'bb';
            //$n['child'] = [];
            $d[$t] = [$n];
            $d[$t]['child'] = [];
            $d = &$d[$t]['child'];
            
        }
        
        //break;
        
    }
}

function re($a)
{
    $c = [];
    $c['menu'] = [];
    foreach($a as $k=>$v)
    {    
        $b = $v;
        $n = $b[0];
        if(!isset($c['menu'][$n]))
        {
            //$b[0]['child']=[];
            $c['menu'][$n][$n]=[$b[0]];
            $c['menu'][$n][$n]['child']=[];
        }
        $d = &$c['menu'][$n];
        unset($b[0]);
        $i = 1;
        $d[$n]['child'] = recursion($b,$d[$n]['child'],$i);
        
        //$c['menu'][$n][$n] = $d;
    }
}

//递归执行的业务就是返回子集 而且数组必须是小到大没有断层的数字排序
function recursion($b,$d,$i)
{
    if(isset($b[$i]))
    {
        $index = $b[$i];
        if(!isset($d[$index]))
        {
            //$b[$i]['child'] = [];
            $d[$index] = [$b[$i]];
            $d[$index]['child'] = [];
        }
        $i++;
        if(isset($b[$i]))
        {
            $d[$index]['child'] = recursion($b,$d[$index]['child'],$i);
        //    return $d[$index]->child;如果这么返回只能接受到最后一个子集
        }
        return $d;
        
    }
        
}


$loop = 0;
$countLoop=100;
$count = 0;
$same = 0;
$a = new PerformanceTest();
$c=$a->buildRandomData(10000);
//print_r($c);exit;
while($loop <$countLoop)
{
    $a->begin();
    re($c);
    $a->end();
    $a1=$a->getSpentTime();

    $a->begin();
    iterator($c);
    $a->end();
    $a2=$a->getSpentTime();
    if($a2<$a1)
    {
        $count++;
    }
    if($a2 == $a1)
    {
        $same++;
    }
    $loop++;
}



var_dump($count);
var_dump($same);
var_dump($countLoop - ($count+$same));


/**
int(100) int(0) int(0)
循环迭代比递归快一个数量级
***/

 

posted on 2017-08-01 15:35  小乔流水人家  阅读(601)  评论(0)    收藏  举报

导航