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,1000000);
}
$total = 0;
$num = 0;
do{
$a->begin();
//$mark = '';
foreach($b as $v)
{
if($v%2 == 0)
{
//$mark = '偶数';
}
else
{
//$mark = '奇数';
}
}
$time1 = $a->end();
$a->begin();
//$mark = '奇数';
foreach($b as $v)
{
if($v%2 == 0)
{
//$even++;
//continue;
}
//$odd++;
}
$time2 = $a->end();
$total++;
if(($time2 - $time1) > 0)
{
$num++;
}
}while($num!=10);
var_dump($num);
var_dump($total);
//int(10) int(429) 429次试验 只有10 if用的时间比 if else多