PHP test program run time class

  • 2020-05-12 02:20:55
  • OfStack

Class is very simple, mainly using several functions array list function list(), string split into array function explode(), get the timestamp and microseconds microtime(), the code is as follows:
 
<?php 
class runTime { 
private $starTime;// The start time  
private $stopTime;// The end of time  
private function getMicTime(){ 
$mictime=microtime();// Gets the timestamp and the number of microseconds  
list($usec,$sec)=explode(" ",$mictime);// Divide the microseconds into arrays and convert them into variables  
return (float)$usec+(float)$sec;// The converted data is forced to be processed by floating point  
} 
public function star(){// Get start time  
$this->starTime=$this->getMicTime(); 
} 
public function stop(){// Get end time  
$this->stopTime=$this->getMicTime(); 
} 
public function spent(){// Calculate the duration of the program  
return round($this->stopTime-$this->starTime)*1000;// Get the number of milliseconds  
} 
} 
// For example,  
$time=new runTime(); 
$time->star(); 
for ($i=1;$i<=1000;$i++){ 
echo("a"); 
} 
$time->stop(); 
echo $time->spent(); 
?> 

Related articles: