複製程式碼程式碼如下:
類別計時器
{
var $time_start; 🎜>var $time_end;
函數__construct()
{
$this->time_start = 0;
$this->time_end = 0;
}
函數計時器()
{
$this->__construct();
}
函數start()
{
list($usec,$sec) = Explode(" ",microtime ());
$this->time_start = (float)$usec + (float)$sec;
}
函數stop()
{
list($usec,$sec ) = Explode(" ",microtime());
$this->time_end = (float)$usec + (float)$sec;
}
function show($output = false)
{
$total = $this->time_end - $this->time_start;
if ($output) {
echo $total," sec";
回傳真;
}}
回傳$total." sec";
}
}
? >
echo '檢查素數
';
函數IsPrime($i)
{
if($i{
回傳false;
}
//var $iterator;
for($iterator = 2 ; $iterator {
if($i % $iterator==0)
{
return錯誤的;
}
}
回傳true;
}
$sw=新計時器();
$sw->start();
for($j=1;$j{
if(IsPrime($j))
{
echo 'true
';
}
else
{
echo 'false
';
}
}
$sw->stop() ;
$sw->show(true);
? >