PHP a page execution time class code

  • 2020-03-31 20:22:32
  • OfStack

The core code


<?php 
class Timer//Page execution time class
{ 
var starttime;//Page start time
var stoptime;//Page end execution time
var spendtime;//Page execution takes time
function getmicrotime()//Gets the floating point number that returns the current number of microseconds
{ 
list(usec,sec)=explode(" ",microtime()); 
return ((float)usec + (float)sec); 
} 
function start()//The page starts to execute the function, which returns the time the page starts to execute
{ 
this->starttime=this->getmicrotime(); 
} 
function display()//Displays the time of page execution
{ 
this->stoptime=this->getmicrotime(); 
this->spendtime=this->stoptime-this->starttime; 
return round(this->spendtime,10); 
} 
} 
/* A method is called  
timer=new Timer(); 
timer->start(); 
 
//Echo "<P> It takes time to execute this code ". Timer -> The display (). "seconds";
?> 

PHP detects the execution time of each piece of code


<?php
//Example 1


function proStartTime() {
  global $startTime;
  $mtime1 = explode(" ", microtime());
  $startTime = $mtime1[1] + $mtime1[0];
}


function proEndTime() {
  global $startTime,$set;
  $mtime2 = explode(" ", microtime());
  $endtime = $mtime2[1] + $mtime2[0];
  $totaltime = ($endtime - $startTime);
  $totaltime = number_format($totaltime, 7);
  echo "<br/>process time: ".$totaltime;
}

//The program call starts to clock
proStartTime();

sleep(1);   //Sleep () delays code execution for a few seconds
proEndTime(); //The elapsed execution time of a program in each segment
sleep(2);
proEndTime();
sleep(3);
proEndTime(); 




//Example 2

$t1 = microtime(true);
sleep(3);
$t2 = microtime(true);
echo ' It takes '.round($t2-$t1,3).' seconds ';

?>


Related articles: