PHP a page Execution time class code _php tips

Source: Internet
Author: User

Core code

<?php class 
timer//page execution time class 
{ 
var starttime;//page start execution time 
var stoptime;//page End Execution Time 
var spendtime ;//page execution takes time 
function getmicrotime ()//Gets the floating-point number 
{ 
list (usec,sec) =explode ("", Microtime ()) 
that returns the current microseconds. Return ((float) usec + (float) sec); 
The function start ()//page begins executing functions, returning the Start page execution time 
{ 
this->starttime=this->getmicrotime (); 
} 
function display ()//Displays the time of the page execution 
{ 
this->stoptime=this->getmicrotime (); 
this->spendtime=this->stoptime-this->starttime; 
Return round (this->spendtime,10); 
} 
/* Call method 
timer=new timer (); 
Timer->start (); 
/* Place the script you want to execute or code for 
(i=0;i<100000;i++) 
{ 
echo i; 
echo "<br>"; 
} 
* * * 
//echo "<p> takes time to execute this code." Timer->display (). " Seconds "; 
? > 

PHP detects every piece of code execution time

<?php
//instance 1

/**
 * @start time
 /
function Prostarttime () {
  global $startTime;
  $mtime 1 = Explode ("", Microtime ());
  $startTime = $mtime 1[1] + $mtime 1[0];
}

/**
 * @End time
/function Proendtime () {
  global $startTime, $set;
  $mtime 2 = Explode ("", Microtime ());
  $endtime = $mtime 2[1] + $mtime 2[0];
  $totaltime = ($endtime-$startTime);
  $totaltime = Number_format ($totaltime, 7);
  echo "<br/>process time:". $totaltime;
}

Prostarttime () When the program call begins to be recorded
;

Sleep (1);   Sleep () Delay code executes several seconds
proendtime ();//The Execution time of the program is consumed in each paragraph
(2);
Proendtime ();
Sleep (3);
Proendtime (); 


/************************************************* Gorgeous split line **************************************************/

Instance 2

$t 1 = microtime (true);
Sleep (3);
$t 2 = Microtime (true);
Echo ' Program time consuming '. Round ($t 2-$t 1,3). ' Seconds ';

? >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.