Methods for calculating page loading time in php _ PHP Tutorial

Source: Internet
Author: User
Summary of several methods for calculating page loading time in php. You can obtain and subtract the start time and end time of the page through the commonly used microtime (). The calculation result is a period of time that the page has been running, however, this is not necessarily because the page itself can get and subtract the start time and end time of the page through the commonly used microtime (). The calculation result is a period of time that the page has been running, however, this is not necessarily the time when the page is running.
The code is as follows:

// Instance: calculates the page runtime loading time
// Analysis: get a time when the page is opened, and get a time when the load is complete. the running time is the difference between the two.

// 1. user-defined functions
Function fn (){
List ($ a, $ B) = explode ('', microtime (); // gets and splits the current timestamp and number of milliseconds, and assigns the value to the variable
Return $ a + $ B;
}

// 2. obtain the start time
$ Start_time = fn ();
// 5. loading process
For ($ I = 0; I I <10000000; $ I ++ ){
// Do nothing;
}

// 3. obtain the end time
$ End_time = fn ();

// 4. calculate the difference value
Echo $ end_time-$ start_time;

// 5. format the output
Echo'
';
$ T = $ end_time-$ start_time;
Echo round ($ t, 2 );

?>

If you use microtime () to obtain and subtract the start time and end time of the page, the calculation result is page running.
This is not necessarily the time when the page runs. Because multiple PHP scripts may exist.
This page is executed together, so I think that method is not accurate


The following is an example of how to calculate the page running time in php from the internet. For more information, see.

Recently I wrote a time computing class for program running for your reference:

The code is as follows:
Class Timer {
Private $ StartTime = 0; // The start time of the program.
Private $ StopTime = 0; // end time of the program running
Private $ TimeSpent = 0; // time spent on running the program
Function start () {// start the program
$ This-> StartTime = microtime ();
}
Function stop () {// The end of the program running
$ This-> StopTime = microtime ();
}
Function spent () {// time spent on running the program
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,). "seconds"; // return the time difference between the obtained programs.
}
}
}
$ Timer = new Timer ();
$ Timer-> start ();
// Code for running the program...
$ Timer-> stop ();
Echo "program Running time:". $ timer-> spent ();

Now let's look at how simple the page loading time for program computing is.

The code is as follows:


Class runtime
{
Var $ StartTime = 0;
Var $ StopTime = 0;
Function get_microtime ()
{
List ($ usec, $ sec) = explode ('', microtime ());
Return (float) $ usec (float) $ sec );
}

Function start ()
{
$ This-> StartTime = $ this-> get_microtime ();
}

Function stop ()
{
$ This-> StopTime = $ this-> get_microtime ();
}

Function spent ()
{
Return round ($ this-> StopTime-$ this-> StartTime) * 1000, 1 );
}
}

// Start the instance
$ Runtime = new runtime;
$ Runtime-> start ();
// Start your code
$ A = 0;
For ($ I = 0; I I <1000000; $ I)
{
$ A = $ I;
}
// Your code ends
$ Runtime-> stop ();
Echo "page Execution time:". $ runtime-> spent (). "millisecond ";
?>

When Begin () gets and subtract the start time and end time of the page, the calculation result is a period of time that the page has been running, but this is not necessarily the operation of the page itself...

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.