php switch 和 ifelseifelseif 相差不大

 

set_time_limit(1200);
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,7);//在这里才能格式化时间
        $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 getTime() 
    {  
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }

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

    public function convert($size)
    { 
        $unit=array('b','kb','mb','gb','tb','pb'); 
        return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i]; 
    } 

}


$a = new PerformanceTest();
$b = [];
for($i=0;$i<1000000;$i++)
{
    $b[] = rand(0,5);
}
$total = 0;
$num = 0;
do{
    
    $a->begin();
    
    foreach($b as $v)
    {
        if($v == 0)
        {
            
        }
        elseif($v == 1)
        {
            
        }
        elseif($v == 2)
        {
            
        }
        elseif($v == 3)
        {
            
        }
        elseif($v == 4)
        {
            
        }
        else
        {
            
        }
    }
    $time1 = $a->end();

    
    $a->begin();

    foreach($b as $v)
    {
        switch ($v)
        {
            case 0:break;
            case 1:break;
            case 2:break;
            case 3:break;
            case 4:break;
            default:;
        }
        
    }
    $time2 = $a->end();
    $total++;
    if(($time2 - $time1) > 0)
    {
        $num++;
    }
}while($num!=10);
var_dump($num);
var_dump($total);


/**
int(10) int(16)
**/

 

posted on 2017-06-10 15:27  小乔流水人家  阅读(174)  评论(0)    收藏  举报

导航