<?php
/*
 * $executionTime = new ExecutionTime('process name');
    $executionTime->start();
    // Code
    $executionTime->end();
    echo $executionTime;
 */
class ExecutionTime
{
     private $startTime;
     private $endTime;
     private $processName;

    public function __construct(string $process)
    {
        $this->processName = $process;
    }

     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->processName ." 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";
     }
 }

