Tôi đã tạo một lớp ExecutTime ngoài câu trả lời phihag mà bạn có thể sử dụng ngoài hộp:
class ExecutionTime
{
     private $startTime;
     private $endTime;
     public function start(){
         $this->startTime = getrusage();
     }
     public function end(){
         $this->endTime = getrusage();
     }
     private function runTime($ru, $rus, $index) {
         return ($ru["ru_$index.tv_sec"]*1000 + intval($ru["ru_$index.tv_usec"]/1000))
     -  ($rus["ru_$index.tv_sec"]*1000 + intval($rus["ru_$index.tv_usec"]/1000));
     }    
     public function __toString(){
         return "This process used " . $this->runTime($this->endTime, $this->startTime, "utime") .
        " ms for its computations\nIt spent " . $this->runTime($this->endTime, $this->startTime, "stime") .
        " ms in system calls\n";
     }
 }
sử dụng:
$executionTime = new ExecutionTime();
$executionTime->start();
// code
$executionTime->end();
echo $executionTime;
  Lưu ý: Trong PHP 5, hàm getrusage chỉ hoạt động trong các hệ thống Unix-oid. Kể từ PHP 7, nó cũng hoạt động trên Windows.