程序占用资源和耗时

时间 内存

 

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]; 
    } 

}

 

posted on 2016-10-21 11:06  小乔流水人家  阅读(160)  评论(0)    收藏  举报

导航