The class code in php that calculates the program's run time

  • 2020-05-26 07:56:49
  • OfStack

 
class Timer { 
private $StartTime = 0;// Program run start time  
private $StopTime = 0;// End of program run time  
private $TimeSpent = 0;// Programs take time to run  
function start(){// Program start  
$this->StartTime = microtime(); 
} 
function stop(){// Program end  
$this->StopTime = microtime(); 
} 
function spent(){// The time the program takes to run  
if ($this->TimeSpent) { 
return $this->TimeSpent; 
} else { 
list($StartMicro, $StartSecond) = explode(" ", $this->StartTime); 
list($StopMicro, $StopSecond) = explode(" ", $this->StopTime); 
$start = doubleval($StartMicro) + $StartSecond; 
$stop = doubleval($StopMicro) + $StopSecond; 
$this->TimeSpent = $stop - $start; 
return substr($this->TimeSpent,0,8)." seconds ";// Returns the obtained program run time difference  
} 
} 
} 
$timer = new Timer(); 
$timer->start(); 
//... The code in which a program is run  
$timer->stop(); 
echo " The running time of the program is :".$timer->spent(); 

Related articles: